Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed!!!

Status
Not open for further replies.

sergelaurent

Technical User
May 30, 2005
52
FR
I have got 2 ".h" file containing 2 class. There are some functions in the second class that I want to use in the first one. How can I do this? Can I use "friend"? Does it works when the 2 class are not in the same ".h" file?
 
Does not matter friend or alien, the only demand: class must been declared (textually) before using its members. For example, if C1 member functions (implemented in separate .cpp file) use C2 members, no .h including order dependencies:
Code:
// c1.h:
class C1 { /* declarations only */ };
// c2.h:
class C2 { /* declarations only */ };
//c1.cpp
#include "c1.h"
#include "c2.h"
... C1:func_member()
{
...
   C2 c2;
   c2.f(...);
}
If you want C2 as a friend of C1, you may use incomplete declaration of C2 in c1.h:
Code:
// c1.h:
class C2; // that's all; declare C2 in another header
class C1
{
...
  friend class C2; // OK
...
};
 
You only need to declare classes as friends if you want those classes to have access to non-public member functions. If the functions are public, anyone in the world can call them as long as they include the header file...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top