-1

C++ is giving me all kinds of headaches. How do i access a private member of another class inside a different class? My example code below, I'm trying to access its members but I keep getting all kinds of errors about it is a private member of another class.

class Ex1 {
private: 
      int test1;
};

class Ex2 {
public:
      Ex2();
      void access2Ex();
private:
      Ex1 *pointer_to_Ex1;
};

I want to access Ex1 from Ex2 and do nAsty stuff to it in Ex2 and by nasty I just want to be able to touch it, caress it, or make it at least acknowledge that Ex2 exists. Anyone can help me out, I'd appreciate it.

user2816227
  • 173
  • 2
  • 13

2 Answers2

0

The entire purpose of a "private" member of a class (be this Java, C++ or any other OO language with the concepts of "public" vs "private") is so other classes can not access them. If you truly want another class to have access, then create an accessor function/property on the class in question:

 class Ex1{
   public:
     int getTest(){ return test1; }
     void setTest(int value){ test1 = value; }
   private:
     int test1;
 }

There, now you have a nice interface that you can program to and take advantage of Object Oriented principals.

Edit: Answer to comment below

 class Ex2{
   public:
     Ex2();
     void printIt(){
       cout << pointer_to_Ex1->getTest() << endl;
     }
 }

So a pointer will be able to call all the public methods of the class it points to. Minor suggestion: use unique_ptr (see standard library) to wrap that pointer and to control the lifetime of the Ex1 pointer.

wheaties
  • 35,646
  • 15
  • 94
  • 131
  • I just got done learning C so C++ concepts are hard to grip. Thank you wheaties. – user2816227 Apr 23 '14 at 15:54
  • @user2816227 Read the "Gang of Four" book. If you're going the way of OO it'll really shed light on to the _why_ of things in OO languages. That said, C++ is anything but a strictly OO language with more ways of doing things that will blow your mind quickly. I say, if you're willing to ask questions (and get a down vote occasionally) then you will learn very quickly. Good luck! – wheaties Apr 23 '14 at 15:57
  • ok so now I've set my accessor functions, how can Ex2 get the information? – user2816227 Apr 23 '14 at 15:58
0

@Wheaties answer is a good one. However I would suggest that rather than simply adding get and set methods you consider adding methods that are meaningful to the purpose of the class.

For example, I worked on a system for libraries once, and rather than having a Loan::setDueDate(date) method we had Loan::renew(date) and Loan::recall(date) methods. Initially they did the same thing: date_ = date but over time they evolved independently so, for example, the renew(date) method could increment the renewCount_ member. (a much better approach than adding setRenewCount(count) and forcing the caller to remember to call both methods when appropriate.)

Encapsulation is an important principle of Object Orientation. Paying attention to it makes your programs more reliable. Disallowing access to private members is the tool that makes ncapsulation work. Setters weaken the tool (although they are much better than allowing direct access to members from "anywhere".)

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52