I have a question of style (i suppose...).
Considering the following simple hierarchy:
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...
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...