Constructor with inherited class

Started by
5 comments, last by Andrew Russell 18 years, 10 months ago
A simple question. I have a base class that initializes a few things in its constructor. I was wondering if this is called automatically in a class derived from the base class, or if I have to call it myself.
____________________________________________________________Programmers Resource Central
Advertisement
Which language?
Don't worry, the constructor of a base class is always called first. But if you didn't provide a default constructor, you must explicitly call the constructor, or the compiler will give you an error.
Thanks.
And just incase we are talknig about a different language I'm using C++, but I suspect that you are also.
____________________________________________________________Programmers Resource Central
Quote:Original post by Tera_Dragon
A simple question. I have a base class that initializes a few things in its constructor. I was wondering if this is called automatically in a class derived from the base class, or if I have to call it myself.


As long as a default constructor (ie. a constructor which takes no arguments) exists in the base class, it will get called without you needing to do anything. However, if no default constructor exists, or if you wish to call a specific constructor you must initialize it.

class base{public:	base();	base(int x);protected:	int myVar;};class derived : public base{public:	derived();	derived(int specialX);};base::base() :myVar(0){}base::base(int x) :myVar(x){}derived::derived(){}derived::derived(int specialX) :base(specialX){}
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Does this just happen with the constructor or with other methods too?
____________________________________________________________Programmers Resource Central
Quote:Original post by Tera_Dragon
Does this just happen with the constructor or with other methods too?

Nope.

Say I have three classes. Base class "A", a class "B" which inherits "A", and then class "C" which inherits "B".

When constructing a "C", the constructors are called in the order: A, B, C.
When destructing, the destructors are called in this order: C, B, A.


Functions, on the other hand, only will be called from one class. If you want to also run one from the parent, you must do so explicitly. For example:

// my classesclass A { void Foo(); };class B : public A{    // B::Foo overrides A::Foo    void Foo()    {        // because it overrides A::Foo, if we want        // A::Foo to run, we must call it explicitly:        A::Foo();    }};


Note the lack of virtual-ness in the above means that if we have a pointer of type "A*" which is pointing to a object that is really of type B, then if we call "Foo()" from that pointer, A::Foo will be run anyway, despite being the "wrong" function (and if we need deconstructors, the lack of a virtual deconstructor will cause us a problem too).

If Foo were made virtual, then if we had a "A* x" pointer that points to a B object, then we could call "x->Foo()" and have it run B::Foo, instead of A::Foo (ie: the correct version for the object). Same goes for running the correct deconstructor.

This topic is closed to new replies.

Advertisement