derived class can not access base class protected member?

This is a discussion on derived class can not access base class protected member? within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do ...

  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
    21,755
    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().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21