Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i write a class tree and inherit a class bst tree.
i want to access both private and public member in class tree.but i have a following error:
cannot access private member declared in class Tree

please help me now!!!!!

The error is pretty explicit: "cannot access private member declared in class"

That is why they are declared as Private - so that you can't access them and mess up the internals of the class.
If you really need to, then you need to talk to whoever created the class, explain why it is so essential for you, and get it changed.

Or find another approach, which is probably a lot easier.
Share this answer
private members are private so inherited classes can't access them. This is by design. Protected would make it possible to access from inherited classes but not when using the class.

There is a very sneaky and dirty way you could use if the specific class member was once visible but now hidden. This works by explicitly casting to the type where the class member was still visible and access it through there. But this shouldn't actually be used because it is bad code design (obviously).

Good luck!
Share this answer
Ok .So how to access both private & public member declared in class inherit?
Share this answer
Richard MacCutchan 17-Jul-12 3:59am    
You cannot access private members; they are private for a reason.
CPallini 17-Jul-12 4:24am    
Using the 'friend' keyword, see my solution for a code sample.
Sorry C# does not provide the friend keyword (see here[^]). As suggested there, you might replace the private keyword with the internal one (if your class is in the same assembly).


Probably I missed the C++ tag:

C++ has the friend keyword (see here[^] for sample code), you may use it.

My silly code sample:
#include <iostream>
using namespace std;

class Derived;
class Base
  friend class Derived; // here Derived is declared 'friend'
  int b;
  Base(int b=0):b(b){}
  void show(){ cout << "b = " << b << endl;}

class Derived: public Base
  void inc(){b++;} // here Derived accesses the private member of Base

int main()
  Derived d;;;;

Share this answer
alirezamilani 17-Jul-12 4:21am    
Ok . In this example, what am i doing ,If i want to access to private member in class base from class derived
CPallini 17-Jul-12 4:47am    
That is distinctly shown in my sample code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900