|
Polymorphism and Inheritance |
Archon
Member #4,195
January 2004
|
I put all objects in "Object[x]" by using the object superclass. How can I tell "Object[x]" to call the cChild classes after it has been constructor-ed as a cChild object? Here is my code:
--- EDIT --- "MAIN(): New Objects - cChild" "MAIN(): Object->something();" "MAIN(): delete Object[]" |
lucaz
Member #4,194
January 2004
|
there are some casting operators, like I think that the array of pointers to cObject, cannot use anything from cChild, unless you define virtual methods or use a cast. |
Thomas Fjellstrom
Member #476
June 2000
|
Don't you have to make the methods virtual? I think that'll fix it. -- |
Archon
Member #4,195
January 2004
|
How would you make a constructor / destructor 'virtual'? ----- EDIT ----- |
Thomas Fjellstrom
Member #476
June 2000
|
I belive so. And maybe make the destructor virtual too... "I think" class BaseClass { public: virtual ~BaseClass(); // not sure if thats valid though, I assume it is. ... }
-- |
Archon
Member #4,195
January 2004
|
OK, I cannot make constructors virtual but I can make destructors virtual.
Output: "MAIN(): Object->something();" "MAIN(): delete Object[]" Notes: With constructors, cObject is called first then cChild's. Code update compared with previous, I am only using 1 object now to make it easier... |
Krzysztof Kluczek
Member #4,191
January 2004
|
1) you can't make constructors virtual since it's completely pointless - how should virtual constructor be different from non-virtual one? ________ |
Archon
Member #4,195
January 2004
|
maybe I should just leave the constructors and destructors empty for superclasses then |
ReyBrujo
Moderator
January 2001
|
In every destructor you should free memory allocated by that constructor. Don't make your child delete memory which was allocated by the base constructor, because you are likely forget to copy that code in another child destructor, generating a memory leak. -- |
gillius
Member #119
April 2000
|
Yeah, use ctors/dtors for what they are useful for. In polymorphism, use them to initialize elements in that class alone. Destructors should be virtual if you ever delete an object through a pointer-to-base type. Usually I make destructors always virtual if I'm using inheritance, just to be safe. Gillius |
23yrold3yrold
Member #1,134
March 2001
|
Quote: OK, I cannot make constructors virtual but I can make destructors virtual. Quite right. Quote: With constructors, cObject is called first then cChild's. Also quite right. Quote: maybe I should just leave the constructors and destructors empty for superclasses then At least initialize all member variables. Wise practice. So the problem is all solved then? -- |
Krzysztof Kluczek
Member #4,191
January 2004
|
ReyBrujo is right. Some example should help understanding it. class cBase { public: char *bunch_of_bytes; cBase() { bunch_of_bytes = new char[100]; } virtual ~cBase() { delete bunch_of_bytes; } }; class cDerived : public cBase { public: char *another_bunch_of_bytes; cDerived() { another_bunch_of_bytes = new char[50]; } virtual ~cDerived() { delete another_bunch_of_bytes; } }; Base class allocates some memory. When derived class constructor is called, it can assume that base class is fully initialized, since its constructor was called and allocated the memory. Now, derived class wants to allocate some more memory. Derived class destructor has only to free memory allocated in derived class constructor, since base class destructor gets called automatically after it and will free base class allocated memory. [edit: forgot ': public cBase' in code] ________ |
decsonic
Member #4,150
December 2003
|
Just be shure you dont do anything like this with your destructors.
You could also implement the base class functions as pure virtual. virtual void Jump(int Height) = 0; Edit : Beaten by a few hours.. ah well. Programmer's paranoia: Don't trust anybody's code, not even your own. |
Tobias Dammers
Member #2,604
August 2002
|
One more rule: If you have any virtual methods in a class, the destructor must be virtual, too. Otherwise, the child's destructor overwrites the parent's one, and the wrong destructors get called. At least, that's what my C++ book says. --- |
23yrold3yrold
Member #1,134
March 2001
|
Quote: One more rule: If you have any virtual methods in a class, the destructor must be virtual, too. Otherwise, the child's destructor overwrites the parent's one, and the wrong destructors get called. At least, that's what my C++ book says. That's right, but for the wrong reason. The destructor should be virtual if you can expect the class to be derived from at some point, and virtual methods are generally a good indicator of that. -- |
gillius
Member #119
April 2000
|
You are not required to make a destructor virtual in any circumstance, but it is good style to make the destructor virtual if any of its methods are virtual. But as long as you always know the exact type of an object when you delete it, then you don't need virtual destructors. But, if you are using virtual methods, then you probably don't know the type at delete time, so if you have virtual methods, 99% of the time you'll want virtual dtors as well. Gillius |
|