Accessing a method from class 1 in class 3

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 49
Reputation: flash121 is an unknown quantity at this point 
Solved Threads: 0
flash121 flash121 is offline Offline
Light Poster

Accessing a method from class 1 in class 3

 
0
  #1
Mar 30th, 2008
Hello,

Lets say i have 3 classes called class1, class2 and class3. Class1 has a method which returns a value, in class2 there's a pointer variable to class1(lets say class1* c1) and in class3 there's a class2 type variable(class2 c2). I would like to access the method from class1 through class3. I've tried c2.c1->method(), which doesnt work because the variables are private. Is there any other way to doing this besides simply making c1 public ?

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Accessing a method from class 1 in class 3

 
0
  #2
Mar 30th, 2008
use friend functions

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class c2;
  5. class c1
  6. {
  7. public:
  8. friend c2;
  9. c1() {}
  10. private:
  11. void SayHello() {cout << "Hello\n";}
  12. };
  13.  
  14. class c2
  15. {
  16. public:
  17. c2() { p1 = new c1;}
  18. ~c2() {delete p1;}
  19. void Hello() {p1->SayHello();}
  20. private:
  21. c1* p1;
  22. };
  23.  
  24. class c3
  25. {
  26. public:
  27. c3()
  28. {
  29. c2 o2;
  30. o2.Hello();
  31. }
  32. };
  33.  
  34. int main()
  35. {
  36. c3 o3;
  37.  
  38. return 0;
  39. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC