Sign in to follow this  
qqicq

How to interact between 2 classes

Recommended Posts

[color=#1C2837][size=2]Hi all,

[/size][/color][color=#1C2837][size=2]I have a class, say A and another class B.

[/size][/color][color=#1C2837][size=2]In class A, I have a method C. inside this method, I will call method D from B
[/size][/color][color=#1C2837][size=2]ie: A::C()
{[/size][/color]







[color=#1C2837][size=2] b.D(A* a) // b is intance of B[/size][/color]
[color=#1C2837][size=2]}[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]But as you see, the method D need a parameter which is a pointer of class A[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]How can I do that?[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]The header files sequence cannot provide me this way to program![/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]Thanks all,
gogogo[/size][/color]
[size="2"][color="#1c2837"]
[/color][/size]

Share this post


Link to post
Share on other sites
Sorry...May be my expression is not good enough...

ok I have 2 classes, one is called A and another is called B.
I have 2 pairs of header and cpp files, A.h, A.cpp, B.h, B.cpp

In class A, I have a method C. Within the method C, I call method D which declare in class B
eg:

void A::C(B* b)
{
b->D(this);
}

which means that C() need to pass a pointer of B and D() need to pass a pointer of A.

The question is, how can I do that?
Because I cannot compile my code since the header file cannot recognize the classes.

eg: If I include A.h first in B.cpp, A.h cannot recognize the class B and vice [font=arial, sans-serif][size=2]versa[/size][/font].

Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this