c++ - 在另一个类中定义的类如何知道它的父类?

标签 c++ class layout

class App
{
    public:
             App(int X, int Y, char* TITLE);
        void run(void);
    public: // Getters
        sf::RenderWindow *getWindow(void);
        sf::Event        *getEvent (void);
        sf::Keyboard     *getKboard(void);
    private: // Variables
        sf::RenderWindow window;
        sf::Event         event;
        sf::Keyboard     kboard;
        #include "game.hpp" // The definition of the Game class
        Game game;
};

主循环。

void App::run(void)
{
    while (window.isOpen())
    {
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) window.close();
            game.run(); // works fine
        }
    }
}

Game类的定义:

class Game
{
    public:
             Game(void);
        void run(void);
};

我希望 Game 类能够访问 App 类的变量。怎么办?

方法一:Game类可以调用App的公共(public)函数。

方法二:Game类可以使用App的protected变量。

class App
{
    public:
             App(int X, int Y, char* TITLE);
        void run(void);
    protected: // Variables
        sf::RenderWindow window;
        sf::Event         event;
        sf::Keyboard     kboard;
    private:
        #include "game.hpp" // The definition of the Game class
        Game game;
};

无论哪种方式,Game 类都需要知道它的父类...那么我该如何配置它的构造函数?

我也可以让游戏类拥有自己的变量,但这完全失去了使用嵌套类的意义...


解决方案:方法三,将Game类声明为App的友元。

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class App
{
    friend class Game; // THE SOLUTION
    public:
             App(const int X, const int Y, const char* NAME); // now using const
        void run(); // not using void to identify empty arguments list anymore
    private: // Variables
        sf::RenderWindow window;
        sf::Event         event;
        sf::Keyboard     kboard;
};
#include "game.hpp"

现在是 game.hpp:

class Game
{
    public:
            // A little bit of redundacy here, but it works!!!
            Game(const int X, const int Y, const char* TITLE) :app(X, Y, TITLE)
            { /* and the initialization of the Game class itself... */}
       void run()
            { app.run(); /* And the running process of Game class itself*/};
     private:
       App app; // Now the Game class owns App and not contrary
 };

最佳答案

了解类之间的关系对您没有帮助,因为您需要一个对象(即实例)才能引用不同的对象。如果您希望一个对象能够访问另一个对象的成员,那么您需要有对另一个对象的引用。

例如,您可以在调用 run 时传递对 App 实例的引用。

class Game
{
    public:
             Game();
        void run(const App& app);
};

或者,如果您愿意,您可以创建一个 App& 类型的私有(private)成员,并在构造函数中初始化该引用。

就个人而言,我认为您没有理由进行嵌套,也没有理由使用不同的头文件。这些类绑定(bind)在一起,所以我将它们放在同一个标​​题中。它可能看起来像这样:

class App; // forward declaration

class Game
{
    public:
        void run(const App& app);
};

class App
{
    public:
             App(const int X, const int Y, const char* TITLE);
        void run();
    private:
        Game game;
};

顺便说一句,使用 void 来指示空参数列表并不是 C++ 惯用的做法。并且您应该尽可能使用 const 以使该类的任何使用者都可以传递 const 对象。

关于c++ - 在另一个类中定义的类如何知道它的父类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20903690/

相关文章:

python - Python中的对象和类有什么区别

c++ - 是否可以从一个基类动态转换到另一个基类?

java - 选择了空值但不在 ComboBoxModel 中的 JComboBox 首选大小

Android:更改设备设置时的布局大小问题

c++ - 在 C++ 上的 Armadillo 中,稀疏矩阵上的 sum(<sp_mat>,<dim>) 不起作用

c++ - 将递增/递减运算符传递给函数

C++ 使用 cppcheck 建议的显式

java - 在 Java 中,如何在方法参数中正确使用抽象类?

layout - Visual Studio Code : Flutter: how to efficiently debug on two simulator at the same time

c++ - 在这种情况下应使用哪种设计模式?