| | |
Accessing a method from class 1 in class 3
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 0
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
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
use friend functions
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class c2; class c1 { public: friend c2; c1() {} private: void SayHello() {cout << "Hello\n";} }; class c2 { public: c2() { p1 = new c1;} ~c2() {delete p1;} void Hello() {p1->SayHello();} private: c1* p1; }; class c3 { public: c3() { c2 o2; o2.Hello(); } }; int main() { c3 o3; return 0; }
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.
![]() |
Similar Threads
- accessing arrays from other classes (C#)
- Help with derived class and arithmetic in C++ (C++)
- Non-static method can't be referenced from a static context (Java)
- problems accessing class instances (Java)
- Accessing a variable of another class (Java)
Other Threads in the C++ Forum
- Previous Thread: Pass Value & Pass-By-Reference
- Next Thread: to generate randomnos
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






