using non-default constructor with inheritence

N

nt8jbwu02

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
 
V

Victor Bazarov

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
 

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,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top