[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

inheritance problem and access to protected members in derived class

Posted on 2005-04-12
7
Medium Priority
?
317 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:PMH4514
  • 4
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
VolatileVoid earned 200 total points
ID: 13763459
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
 

Author Comment

by:PMH4514
ID: 13763469
oh yeah, duh!
0
 

Author Comment

by:PMH4514
ID: 13763474
>>so your ctor needs to allocate memory for m_pList

or call the parent class constructor, which allocates the memory
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:Axter
ID: 13763508
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
 
LVL 30

Expert Comment

by:Axter
ID: 13763524
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
 
LVL 30

Expert Comment

by:Axter
ID: 13763539
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
 
LVL 30

Expert Comment

by:Axter
ID: 13763624
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question