inheriting constuctors

Hi guys

I'm watching John Purcells videos on c++,

he says that constuctors are not inherited by the sub class of a super class but the super class's constructor is called when that class is instaniated

how is this possible? if the super classes constructor is getting called ?

what is the difference between inheriting and calling in this case?

thanks


The language used in that description is a little murky there...

The derived class gets access to all public and private symbols in the superclass, including the superclass’s constructors.

However, not all of those symbols are automatically visible in the derived class; constructors in particular.

This is because a constructor is an unusual kind of function — it really isn’t like other member functions. A constructor is responsible for building an object.

A derived class really is a different kind of thing than the superclass, so a derived class’s constructor(s) really should be different things than the superclass’s constructors.

That is, I shouldn’t be able to create a <derived> with a <super> constructor.
I should need a <derived> constructor to create a <derived>.

In reality that is not always the case, so you often need to just pass construction back up the chain of constructors. This happens automagically if you don’t otherwise specify.

struct A;
struct B: public A;

When constructing a B, the compiler must first build an A (using an A constructor), then let B’s constructor do whatever is necessary to turn A into a B.

Er, I’m beginning to ramble, so...

Topic archived. No new replies allowed.