Derived and base class question

J

Jeroen

Hi,

I have 2 questions that I could not find an answer for yet. Suppose I
have the following code (only illustrative...):

class A {
A(int parm = 0);
~A();
};

class B : public A {
B();
~B();
};

Question1: If I create an object of type B, then the ctor is
automatically called for A without passing arguments. How (where) do I
change the code such that I can give a parameter to the ctor of A when
creating an object of type B?

Question 2: If I have a vector:

std::vector<A*> A_pointers;

then am I allowed to store pointers to B in it also?

Thanks,

Jeroen
 
S

Stefan Naewe

Hi,

I have 2 questions that I could not find an answer for yet. Suppose I
have the following code (only illustrative...):

class A {
A(int parm = 0);
~A(); virtual ~A();
};

class B : public A {
B(); B(int parm) : A(parm) { }
~B();
};

Question1: If I create an object of type B, then the ctor is
automatically called for A without passing arguments. How (where) do I
change the code such that I can give a parameter to the ctor of A when
creating an object of type B?

Question 2: If I have a vector:

std::vector<A*> A_pointers;

then am I allowed to store pointers to B in it also?

Yes. But make the d'tor of A virtual (s. above).


S.
 
J

Jeroen

Stefan Naewe schreef:
Yes. But make the d'tor of A virtual (s. above).


S.

OK, thanks. For question 1 I meant that when constructing an object B,
the ctor of A always should be called with a certain parameter. I quess
I have to achieve that with:

class B : public A {
B() : A(31515) { } // or whatever parameters I want here...
~B();
};

Can you give me a clue why the dtor of A must be virual? Is that only
'better coding practice' or mandatory by C++?

Jeroen
 
S

Stefan Naewe

Stefan Naewe schreef:

OK, thanks. For question 1 I meant that when constructing an object B,
the ctor of A always should be called with a certain parameter. I quess
I have to achieve that with:

class B : public A {
B() : A(31515) { } // or whatever parameters I want here...
~B();
};

Yes, that's the way to go (if 31515 is the desired value...)
Can you give me a clue why the dtor of A must be virual? Is that only
'better coding practice' or mandatory by C++?

http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7


S.
 
J

James Kanze

The reason that the destructor must be virtual for A is that if you
have code like A* p =new B; then we delete p, then the destructor for
B should be called to correctly. If you do not have the destructor in
A virtual this cannot occur and A's destructor will only called and
not the objects destructor that p points to.

Maybe. If you do "delete p", and the static type of *p is not
the same as the dynamic type of *p, it is undefined behavior.
For very simple cases, like the above, you will generally end up
just not calling the destructor of the derived class, but this
is not guaranteed, and in more complicated cases, stranger
things (e.g. core dumps) really do happen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top