Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Derivation and base class member initialization

Status
Not open for further replies.

Nosferatu

Programmer
Jun 9, 2000
412
RO
I have a question of style (i suppose...).

Considering the following simple hierarchy:
Code:
class Base {
  someMember member;
public:
  Base(someMember init) {
     member = new someMember(init); // ??? or :
     member = init;
  ...
};

class Derived : public Base {
   Derived(someMember sM) : Base(sM) {}  // ??? or
   Derived() : Base(new someMember(sM)) {}
   ...
}

What i dont' know for sure, in hierarchies like this, where you want to initialize and use members of the base class, which is the best approach to use?

1. Pass the parameters of the object someMember to the base Class and let the base class handle its initialization? What if the initialization of someMember requires a lot of arguments and, STILL want to use the initialization in the construction process?

2. Create the object separately and assign only references to it. I don't think this approach is worthy, because the reference lives outside the object, so the object has no control over it. Who should destroy the object? Obviously if any of the classes in the hierarchy will do that, the program using them just might crash if it uses the class passed as pointer to the constructor... or whatever... I wouldn't want to use that.

So, how the data should be allocated/dealocated used in class hierarchies? Should the base class perform all the management of the objects (i think so) and provide derived classes with methods to set/reset the data they contain?

I wonder if i already answered my question... Anyway, i would welcome any comments you guys would like to make!
Thanks!

This question is posed in both OOP and C++ forums. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
Nos,

I guess u answered your question. The base class should in reality handle the management of those data members that is universally applicable in that design.
I would move the management of elements that are particular to the derived in the derived class.

Good question

Srik
 
class Base {
someMember member;
public:
Base(someMember init) {
member = new someMember(init); // ??? or :
member = init;
...
};

class Derived : public Base {
Derived(someMember sM) : Base(sM) {}// ??? or
Derived() : Base(new someMember(sM)) {}//where will compiller know about this sM???
//by the way, new someMember returns a pointer
// to someMember, not a someMember, maybe you need
// *(new someMember) but you will have there a memory leak
//better is to put a edfault value
//like Derived(someMember sM = something) : Base(sM){}
//and the second constructor become useless


...
} Ion Filipski
1c.bmp


filipski@excite.com
 
@Ion:
Oooops... I think i was hungry and "eaten" the definition of the second constructor. it should had the same form as the first one, notice the or i put there btween the constructors in the derived class and in the constructor of the base class.
Have you read anyway the questions? I really have a question of choice here... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
By using default values you create a safe constructor with safe initializations. By the way, native C++ types also have ctors:
int i(0);//constructor by value
int x = int(0);//copy constructor + constructor by value
int xx(x);//copy constructor
int xxx = int(xx);//copy constructor + constructor by value
int y = *(new int(100));//be attentive, very bad method

The best approach is to use less operator new. But anyway, put correct syntax, even if you put it in the forum, not in the compiller:

someMember member;
member = new someMember(init);
is sintactically incorrect see two sintactically correct constructions:
first:
someMember* member;
member = new someMember(init);
//second ctor from second class
Derived(someMember sM = something) : Base(new someMember(sM)) {}

second which genearte memory leaks impossible to handle:
someMember* member;
member = *(new someMember(init));
//second ctor from second class
Derived(someMember sM = something) : Base(new someMember(sM)) {}

the safest:
someMember* member;
member = new someMember(init);
//second ctor from second class
Derived(someMember sM = something) : Base(someMember(sM)) {}//without new!!!
Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top