Madalin Neacsu
Madalin Neacsu

Reputation: 25

Two classes with member functions taking a pointer to an object from the other class

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 such a way that I can use function f2?

Upvotes: 1

Views: 60

Answers (1)

Marcus
Marcus

Reputation: 1930

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);
};

Upvotes: 2

Related Questions