Click here to Skip to main content
13,506,337 members
Rate this:
Please Sign up or sign in to vote.
See more:

My problem is like this

static CommandList* commandList = 0;
static Networker* networker = 0;

void f1()

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
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".

1 solution

Rate this: bad
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
  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
  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(...)

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.
Nice explanation, a 5.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180417.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2018
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