具有成员函数的两个类从另一个类获取指向对象的指针

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

本文关键字:另一个 获取 取指 指针 对象 函数 成员 两个      更新时间:2023-10-16
class a {
public:
void f2(b * elem);
};
class b {
public:
void f1(a * elem);
};

这里会有一个问题。

void f2(b * elem);

如何以可以使用函数f2的方式声明类b

告诉编译器有一个类a和b。但不要告诉他/她它们的样子:(这是可能的,因为你使用指针。它只是编译器的一个整数。稍后,您可以定义类的所有函数,编译人员现在很乐意知道它们的样子。

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);
};
相关文章: