Derived and base class question

Discussion in 'C++' started by Jeroen, May 23, 2007.

  1. Jeroen

    Jeroen Guest

    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
    Jeroen, May 23, 2007
    #1
    1. Advertising

  2. Jeroen

    Stefan Naewe Guest

    On 5/23/2007 11:44 AM, Jeroen wrote:
    > 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.
    --
    Stefan Naewe stefan dot naewe at atlas-elektronik dot com
    Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
    Plain text mails only, please http://www.expita.com/nomime.html
    Stefan Naewe, May 23, 2007
    #2
    1. Advertising

  3. Jeroen

    Jeroen Guest

    Stefan Naewe schreef:
    > On 5/23/2007 11:44 AM, Jeroen wrote:
    >> 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.


    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
    Jeroen, May 23, 2007
    #3
  4. Jeroen

    Stefan Naewe Guest

    On 5/23/2007 12:20 PM, Jeroen wrote:
    > Stefan Naewe schreef:
    >> On 5/23/2007 11:44 AM, Jeroen wrote:
    >>> 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.

    >
    > 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.
    --
    Stefan Naewe stefan dot naewe at atlas-elektronik dot com
    Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
    Plain text mails only, please http://www.expita.com/nomime.html
    Stefan Naewe, May 23, 2007
    #4
  5. Jeroen

    Jeroen Guest

    Stefan Naewe schreef:
    > On 5/23/2007 12:20 PM, Jeroen wrote:
    >> Stefan Naewe schreef:
    >>> On 5/23/2007 11:44 AM, Jeroen wrote:
    >>>> 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.

    >> 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.

    OK, thanks guys!
    Jeroen, May 23, 2007
    #5
  6. Jeroen

    James Kanze Guest

    On May 23, 12:34 pm, Keith Halligan <> wrote:
    > On May 23, 11:20 am, Jeroen <> wrote:


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


    > 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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 24, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    393
    myork
    May 23, 2007
  2. Replies:
    1
    Views:
    385
    Victor Bazarov
    May 23, 2007
  3. Replies:
    2
    Views:
    706
  4. Replies:
    1
    Views:
    220
    Hans Mulder
    Nov 6, 2012
  5. Replies:
    7
    Views:
    217
    alex23
    Nov 7, 2012
Loading...

Share This Page