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?
PMH4514Asked:
Who is Participating?
 
VolatileVoidConnect With a Mentor Commented:
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
0
 
PMH4514Author Commented:
oh yeah, duh!
0
 
PMH4514Author Commented:
>>so your ctor needs to allocate memory for m_pList

or call the parent class constructor, which allocates the memory
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
AxterCommented:
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!
0
 
AxterCommented:
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 :-}
0
 
AxterCommented:
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.
0
 
AxterCommented:
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 :-}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.