C++ Struct inheritance against class inheritance

Discussion in 'C++' started by johnsonlau, Jul 21, 2008.

  1. johnsonlau

    johnsonlau Guest

    When a class derives from a class,
    You can use a pointer to the parent class to delete the instance of
    child
    only when a virtual destructor declared in the parent.

    class Parent
    {
    virtual ~Parent(); // virtual destructor
    }

    class Child : public Parent
    {
    }

    Parent * instance = new Child();
    delete instance;

    ===============================================

    But is it the same when parent is a struct?

    struct StructParent {
    }

    class Child : public StructParent {
    }

    StructParent * instance = new Child();
    delete instance;

    Does this mean that I should decleare a virtual destructor in
    StructParent
    to provide correct information about the parent and a safe delete
    operation?
    Or I can only write codes like:
    Child * instance = new Child();
    delete instance;

    I'm a little confused.

    Can I say that,
    if I ensure that I only use pointer to the Child (bug not the
    Parent's) and
    perform delete operation on it, plus there is no virtual method in
    both Parent and Child,
    I can define no virtual destructor in struct inheritance and class
    inheritance.

    Thank you.
    johnsonlau, Jul 21, 2008
    #1
    1. Advertising

  2. johnsonlau

    Kai-Uwe Bux Guest

    johnsonlau wrote:

    > When a class derives from a class,
    > You can use a pointer to the parent class to delete the instance of
    > child
    > only when a virtual destructor declared in the parent.
    >
    > class Parent
    > {
    > virtual ~Parent(); // virtual destructor
    > }
    >
    > class Child : public Parent
    > {
    > }
    >
    > Parent * instance = new Child();
    > delete instance;
    >
    > ===============================================
    >
    > But is it the same when parent is a struct?


    Yes, it is the same.

    > struct StructParent {
    > }
    >
    > class Child : public StructParent {
    > }
    >
    > StructParent * instance = new Child();
    > delete instance;


    This is undefined behavior as you suspected.


    > Does this mean that I should decleare a virtual destructor in
    > StructParent
    > to provide correct information about the parent and a safe delete
    > operation?


    That is one way.


    > Or I can only write codes like:
    > Child * instance = new Child();
    > delete instance;


    That is fine, too.


    > I'm a little confused.
    >
    > Can I say that,
    > if I ensure that I only use pointer to the Child (bug not the
    > Parent's) and
    > perform delete operation on it, plus there is no virtual method in
    > both Parent and Child,
    > I can define no virtual destructor in struct inheritance and class
    > inheritance.


    Huh? Of course you can define a virtual destructor. However, in the case you
    described, you don't have to.

    What you need to keep in mind is that a virtual destructor is needed
    whenever you delete an object of derived type through a pointer to a base.
    It does not matter whether the type its a struct or a class nor whether it
    has other virtual methods or not.

    BTW: structs and classes in C++ only differ with regard to the default
    access; structs are public by default and classes have private access by
    default.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jul 21, 2008
    #2
    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. E11
    Replies:
    1
    Views:
    4,717
    Thomas Weidenfeller
    Oct 12, 2005
  2. RA Scheltema
    Replies:
    3
    Views:
    381
    RA Scheltema
    Jan 6, 2004
  3. Gunnar G

    struct in struct

    Gunnar G, May 31, 2004, in forum: C++
    Replies:
    14
    Views:
    788
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,357
    Chris Fogelklou
    Apr 20, 2004
  5. Replies:
    2
    Views:
    727
    David Harmon
    Sep 20, 2006
Loading...

Share This Page