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

标签 c++

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

关于c++ - 两个具有成员函数的类从另一个类获取指向对象的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38717088/

相关文章:

c++ - 随机化 std::list 时提高性能

c++ - 为什么它不能在 VS Express 2013 中编译,尽管它在 MinGW 中编译?

c++ - 在 C++ 中初始化引用

c++ - 如何将此程序集时间戳函数转换为 C++?

c++ - 找不到生成的 header

C++ 从数组中取出前 n 个元素

c++ - 将一系列已排序元素划分为相邻的组

c++ - Xcode上找不到'opencv2/core/core.hpp'文件错误

c++ - 在 C++(WinAPI 或 QT)中打印文本和图像

c++ - 项目符号 2.81 : Unable to find installed header?