我需要创建一个指向类对象的指针。但是,它首先声明为nullptr。我需要指出那个班级。
在这里,我将它们声明为nullptr:
#pragma once
#include "Window.h"
#include "Game.h"
#include "Map.h"
#include "Input.h"
class SN {
public:
SN();
Window * window = nullptr;
Game * game = nullptr;
Map * map = nullptr;
Input * input = nullptr;
};
在这里,我尝试将它们分配给它们的对象:
#include "SN.h"
SN::SN(){
Game * game(this); //I WAS TRYING TO DO THIS BUT IT ALSO DID NOT WORK
Window window(this);
Map map(this);
Input input(this);
}
我将SN的对象传递给他们的构造函数,所以他们也可以使用SN.h。 请帮助我,提前谢谢。
答案 0 :(得分:2)
你的意思是?
SN::SN(){
game = new Game(this);
window = new Window(this);
map = new Map(this);
input = new Input(this);
}
注意:使用new
创建的对象永远不会被自动销毁;如果你想要销毁它们,你必须使用delete
。
SN::~SN(){
delete game;
delete window;
delete map;
delete input;
}