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 wOOdy-Soft 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 (i really think the OOP forum should begin to get more visited!). [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
 
1. In ohter words, you inherit there the constructor of basic class. By default is inherited the default constructor.

class a
{
int x;
public:
a(){cout<<&quot;default constructing of a&quot;;}
a(int c = 0){x = c;cout<<&quot; + constructing a &quot;<<c;}
};
class b:public a
{
public:
b():a(0){cout<<&quot; + default constructing b &quot;<<endl;}
//and try b(){... instead of b():a(){...
b(int d):a(d){cout<<&quot; + constructing b &quot;<<endl;}
//you can say it is an inheritance of the constructor
};
int main(int argc, char* argv[])
{
b e,d,x(4);
return 0;
}

it the object requires many arguments, put it in the initialization list, for example a reference require at least one

class a
{
int x;
int& rx
public:
a():rx(x){...}
b():rx(x){...}
//you must initialize the reference in
//the initialization list of each constructor

};

all automatic objects inside a class are destroyed automatically when class destructor is called.
for example, you can monitorize them as
class show
{
public:
show(){cout<<&quot;see construct&quot;<<endl;}
~show(){cout<<&quot;see destruct&quot;<<endl;}
};
put its instances inside a class and see the mechanisme of constructing/destructing. Ion Filipski
1c.bmp


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

Part and Inventory Search

Sponsor

Back
Top