Sign in to follow this  
Tera_Dragon

Constructor with inherited class

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)
{
}

Share this post


Link to post
Share on other sites
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 classes
class 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this