A类需要B类,需要A类及其所有成员

Ele*_*ntW 2 c++ recursion class include members

我绝对是一个绝望的案例......使用C++,我已经定义了这两个类:

// Console.hpp
#ifndef CLASS_Console
#define CLASS_Console
#include <iostream>
#include "Window.hpp"

class Console
{
public:
    Window *Win;
    Console(Windows *Win); // Which then does "this->Win = Win;"
    void AppendText(std::string);
    // And some more dozens functions
};
#endif

// Window.hpp
#ifndef CLASS_Window
#define CLASS_Window
#include <iostream>
#include "Size.hpp"

class Window
{
private:
    Console *m_Console;

public:
    Window(); // Which then does "m_Console = new Console(this); m_Console->AppendText("YEAH");"
    Console *getConsole(); // For use by another classes
    Size getSize();
    // And some more Tens and Tens of functions
};
#endif
Run Code Online (Sandbox Code Playgroud)

首先,人们会说:"使用前瞻声明!"

但是鉴于我需要访问许多这些功能(在一种方式和另一种方式中),有什么办法可以避免使用前向声明吗?

谢谢你(未来)的答案!

Dav*_*eas 5

但是鉴于我需要访问许多这些功能(在一种方式和另一种方式中),有什么办法可以避免使用前向声明吗?

我不确定你理解前方声明的作用.你只需要提供一个向前声明window.hppConsole,然后包括console.hppwindow.cpp.