PMH4514
asked on
inheritance problem and access to protected members in derived class
What would be a reason that a derived class doesn't seem to have direct access to the protected members in a parent class unless prefacing it with the namespace?
ie:
class a
{
public:
a()
{
m_pList = new CPtrList();
}
virtual ~a();
protected:
CPtrList* m_pList;
};
#include "a.h"
class b : public a
{
public test()
{
CFoo oFoo = new CFoo();
m_pList->AddTail(oFoo); // this line fails as m_pList = 0xcccccccc
a::m_pList->AddTail(oFoo); // this works
}
}
am I declaring something wrong?
ie:
class a
{
public:
a()
{
m_pList = new CPtrList();
}
virtual ~a();
protected:
CPtrList* m_pList;
};
#include "a.h"
class b : public a
{
public test()
{
CFoo oFoo = new CFoo();
m_pList->AddTail(oFoo); // this line fails as m_pList = 0xcccccccc
a::m_pList->AddTail(oFoo);
}
}
am I declaring something wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>so your ctor needs to allocate memory for m_pList
or call the parent class constructor, which allocates the memory
or call the parent class constructor, which allocates the memory
Hi PMH4514,
> >class b : public a
> >{
> > public test()
> > {
> > CFoo oFoo = new CFoo();
> > m_pList->AddTail(oFoo); // this line fails as m_pList = 0xcccccccc
> >
> > a::m_pList->AddTail(oFoo); // this works
> > }
> >}
Try add : to the end of public, adn you need a return type for test.
David Maisonave :-)
Cheers!
> >class b : public a
> >{
> > public test()
> > {
> > CFoo oFoo = new CFoo();
> > m_pList->AddTail(oFoo); // this line fails as m_pList = 0xcccccccc
> >
> > a::m_pList->AddTail(oFoo);
> > }
> >}
Try add : to the end of public, adn you need a return type for test.
David Maisonave :-)
Cheers!
VolatileVoid,
> >If I recall correctly, a derived class's constructor doesn't call the
> >parent class constructor, so your ctor needs to allocate memory for m_pList
That's not true.
The default parent contstructor will get called automatically.
David Maisonave :-}
> >If I recall correctly, a derived class's constructor doesn't call the
> >parent class constructor, so your ctor needs to allocate memory for m_pList
That's not true.
The default parent contstructor will get called automatically.
David Maisonave :-}
class b : public a
{
public:
void test()
{
CFoo oFoo = new CFoo();
m_pList->AddTail(oFoo); // this line fails as m_pList = 0xcccccccc
a::m_pList->AddTail(oFoo); // this works
}
};
Try the above code. It should work via both methods.
The reason it might have failed with original code, is becuase the compiler is getting confuse since there is no return type for the function.
{
public:
void test()
{
CFoo oFoo = new CFoo();
m_pList->AddTail(oFoo); // this line fails as m_pList = 0xcccccccc
a::m_pList->AddTail(oFoo);
}
};
Try the above code. It should work via both methods.
The reason it might have failed with original code, is becuase the compiler is getting confuse since there is no return type for the function.
VolatileVoid,
> >a derived class's constructor doesn't call the parent class constructor
FYI:
IAW C++ standard, a derived class constructor can not be called without first calling the parent constructor.
David Maisonave :-}
> >a derived class's constructor doesn't call the parent class constructor
FYI:
IAW C++ standard, a derived class constructor can not be called without first calling the parent constructor.
David Maisonave :-}
ASKER