Thread: derived class can not access base class protected member?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    derived class can not access base class protected member?

    Hello everyone,


    I met with a strange issue that derived class function can not access base class's protected member. Do you know why?

    Here is the error message and code.

    Code:
    error C2248: 'base::~base' : cannot access protected member declared in class 'base'
    Code:
    class base
    {
    protected:
    	~base() {}
    private:
    	void foo()
    	{
    		base* b = new base;
    		delete b;
    	}
    };
    
    class derived : public base
    {
    public:
    	~derived() {}
    private:
    	void goo()
    	{
    		base* b = new derived;
    		delete b; // error in this line
    	}
    };

    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is not so clear when you invoke the destructor via delete, so perhaps try out this example instead:
    Code:
    class base
    {
    protected:
    	void foo()
    	{
    	}
    };
    
    class derived : public base
    {
    public:
    	void goo()
    	{
    		base* b = new derived;
            b->foo();
    		delete b;
    	}
    };
    If you want it even clearer, change derived to:
    Code:
    class derived : public base
    {
    public:
    	void goo()
    	{
    		base b;
            b.foo();
    	}
    };
    So the derived object is not trying to access foo() as a derived class, but as if it were not related to base or as if goo() was a free non-friend function calling b.foo().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    First of all, your base class destructor should be virtual!

    Next, your derived class can call protected members of the base class directly, but not through a pointer like that.

    In LaserLight's example, calling b->foo() will still fail, but you can call foo() just by itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  2. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Can't Access the private member from base class
    By planet_abhi in forum C# Programming
    Replies: 3
    Last Post: 01-09-2006, 04:30 AM
  5. base class pointer to derived class objects
    By curlious in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2003, 08:39 PM