Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

My problem is like this

Main.cpp
static CommandList* commandList = 0;
static Networker* networker = 0;
...

void f1()
{
 commandList->RunCommands();
}

void f2()
{
 networker ->Listen();
}


in the class Networker class I want to call "commandList" functions and in the CommandList, I want to call "networker" functions

how to do this ?
I tried to find a solution with Google. I saw the forward declaration. It's maybe the solution but I don"t know what to change

Many thanks for your help
Posted
Comments
Sergey Alexandrovich Kryukov 3-Feb-13 19:25pm    
Please don't post inappropriate content using "Add your solution here", which is reserved only for the cases when you are trying to provide some help, in response to some question.

Instead, comment on other posts, reply to existing comments, use "Improve question".
—SA

1 solution

The secret to getting things in the right order in C++ is to think in terms of translation units rather than files.
A translation unit is a cpp file like your main.cpp with all the included headers pasted in, just as if you'd copied and pasted the content of each file into the exact place where you have a #include for it and then done that again with the resulting code until the whole thing is one huge file. This craziness is exactly what the C preprocessor does with your code as a first step before compiling really gets going.
This pasted together super file is the thing that actually gets compiled and its the order of things in there that matters.
There's not enough of your code in your post for me to tell exactly whats wrong but a very general solution often ends up looking something like this.
//file A.h

class B;//This is a forward declaration of B

class A
{
...
private:
  B* m_pb;/*pointers and references only to B can be declared, no actual code that uses them though*/
};

//file B.h

class A;/*The compiler has already seen this but it doesn't matter as long as it matches the declaration already seen*/

class B
{
...
private:
  A* m_pa;/*We could use an actual A here but if inclusion order changes things will break so we don't*/
};

//file A.cpp

int A::somefunc(...)
{
  m_pb->DoABOperation();
}


That's the compilation unit for A.cpp. As you can see the one for B.cpp will be similar assuming that the A.cpp and B.cpp files as typed each begin with:
#include "A.h"
#include "B.h"
...


Forward declaration and the way inclusion works is a very old fashioned and cranky but very powerful tool for doing all sorts of things once you get a handle on it. Removing it from C# was one of the reasons I tend to stick with C++. Probably because I'm old fashioned and cranky too.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Feb-13 19:23pm    
Nice explanation, a 5.
—SA
Philippe Mori 4-Feb-13 21:45pm    
Not having to deal with include and forward declaration in C# is a huge step forward. Even better, C# compiler is much faster than C++ compiler.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900