Madalin Neacsu Madalin Neacsu - 4 months ago 7
C++ Question

I want to make 2 classes, each of which has a member function that takes a pointer to an object of the other class. How do i do that?

class a {
public:
void f2(b * elem);
};

class b {
public:
void f1(a * elem);
};


There will be a problem here.

void f2(b * elem);


How do i declare class b in that way that i can use function f2 ?

Answer

tell the compiler that there is a class a and b. But don't tell him/her how they look like :) It is possible because you work with pointers. Its only a integer for the compiler. Later you can define all the functions of the classes and the compile will be happy to know now how they look like.

class a; // tell the compiler there is a class a
class b; // tell the compiler there is a class b

// real implementation of class a
class a {
    public:
        void f2(b * elem);
};

// real implementation of class b
class b {
    public:
        void f1(a * elem);
};