C++ 如何将指针重新分配给以 this 作为参数的对象?

C++ How to reassign a pointer to an object with this as argument?

我需要创建一个指向 class 对象的指针。但是,它首先被声明为 nullptr。我需要指出 class.

在这里,我将它们声明为 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。 请帮助我,在此先感谢。

你是这个意思吗?

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