TPECI

Hello,
I have 2 classes in one namespace and each of these needs to have an instance of the other, like below:

namespase myspace
{
ref class a
{
int fun1(){ b ^ instanceof_b;}

};

ref class b
{
int fun2(){ a ^ instanceof_a;}

}

}
...................................................
why it dont work and what should do to have such a relations


Re: Visual C++ General Problem With Classes

Ramkrishna Pawar

And your problem is..... If you are gettting compiler error class b not defined... then you need to put a fw delcaration like,

Code Snippet

namespase myspace
{

class b;
ref class a
{
int fun1(){ b ^ instanceof_b;}

};

ref class b
{
int fun2(){ a ^ instanceof_a;}

}

}






Re: Visual C++ General Problem With Classes

einaros

TPECI wrote:
Hello,
I have 2 classes in one namespace and each of these needs to have an instance of the other, like below:
[...]

why it dont work and what should do to have such a relations

First off: cyclic dependencies will reduce your code quality. It will, in the long run, make your application difficult to extend, maintain, and possibly bug-analyze/fix. In the extent possible, you should avoid all such chains, and attempt to replace them with other approaches. One way in particular to do this, is to provide the "child" class with callbacks to the various functions it depends on. The child class would no longer depend on the parent class, but rather a set of assignable functions, which can connect it to just about any other class. Maintaining such a layout is a great deal easier than the former.

As for your actual problem, the errors you are seeing will be related to the 'b' type not yet having been defined, as 'a' is being compiled. The compiler will not attempt to look further down in the source to find a definition, so in order to have it all compile, you will have to pre-declare the 'b' class prior to defining class 'a'.