Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 3-Feb-13 9:06am
canard291.3K
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 3-Feb-13 19:23pm
   
Nice explanation, a 5.
—SA
Philippe Mori at 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)

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 201
2 PhilLenoir 164
3 Magic Wonder 162
4 Richard MacCutchan 160
0 Sergey Alexandrovich Kryukov 6,140
1 OriginalGriff 5,195
2 CPallini 2,473
3 Richard MacCutchan 1,607
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100