是否可以将类的实例作为另一个类的数据成员?

Isa*_*tto 2 c++ oop

我有一个Board类,其中构造函数将板的尺寸作为参数.我还有一个Puzzle包含片段的类,我希望它有一个Board作为数据成员.我希望它像这样,所以当我创建一个实例时Puzzle,我将Board创建我的实例,所以我不必作为用户创建单独的实例.但是,当我在我的Puzzle.h文件中声明该板时,它需要Board构造函数的实际数字:

// Puzzle.h file

private:
  Board theBoard(int height, int width); // Yells at me for not having numbers
Run Code Online (Sandbox Code Playgroud)

如果尚未创建该对象,是否有办法让类的对象成为另一个类的数据成员?

Ree*_*sey 6

如果我理解正确,问题是您需要正确实例化您的电路板:

 class Puzzle {
 public:
       Board theBoard;

       Puzzle(int height, int width) : theBoard(height, width) // Pass this into the constructor here...
       {
       };
 };
Run Code Online (Sandbox Code Playgroud)