using non-default constructor with inheritence

Discussion in 'C++' started by nt8jbwu02@sneakemail.com, Nov 15, 2006.

  1. Guest

    Here is a simple version of my classes.

    class BaseClass
    {
    protected:
    BaseClass *myParent;
    public:
    BaseClass(BaseClass *parent);
    virtual ~BaseClass();
    };

    class DerivedClass : BaseClass
    {
    public:
    DerivedClass(BaseClass * parent);
    virtual ~DerivedClass();
    };

    BaseClass::BaseClass(BaseClass *parent)
    {
    myParent = parent;
    }

    BaseClass::~BaseClass()
    {
    // clean up
    }

    DerivedClass::DerivedClass(BaseClass * parent):BaseClass(*parent)
    {
    // init derived class
    }

    DerivedClass::~DerivedClass()
    {
    // clean up
    }

    ////
    instantiation is:
    DerivedClass *test = new DerivedClass(NULL);

    Everything compiles but when I try and execute, I get an access
    violation. Do you see what I have done wrong?

    Thanks,
    Eric
    , Nov 15, 2006
    #1
    1. Advertising

  2. wrote:
    > Here is a simple version of my classes.
    >
    > [..]
    > DerivedClass::DerivedClass(BaseClass * parent):BaseClass(*parent)


    You probably meant

    ... : BaseClass(parent)

    > {
    > // init derived class
    > }
    > [..]
    >
    > Everything compiles but when I try and execute, I get an access
    > violation. Do you see what I have done wrong?


    You attempt to dereference a null pointer.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 15, 2006
    #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. Aire
    Replies:
    3
    Views:
    450
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    9
    Views:
    938
    Alf P. Steinbach
    Mar 6, 2006
  3. toton
    Replies:
    6
    Views:
    238
    toton
    Nov 15, 2006
  4. desktop
    Replies:
    2
    Views:
    442
    Victor Bazarov
    Jun 6, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,184
Loading...

Share This Page