C ++我能用nullptr对象调用函数吗?

时间:2015-12-03 15:14:08

标签: c++ pointers constructor

我有一个奇怪的问题。我可以使用指向我创建的对象的指针调用Window类(命名为test)中的函数,但是当我输出指针时它是null(输出:00000000)。这甚至可能吗?这可能有用,但其他课程不能。

这是我声明指针的地方(正常,对吗?):

#pragma once
#include "Window.h"
#include "Game.h"
#include "Map.h"
#include "Input.h"

class SN {
public:
    SN();
    Window * window;
    Game * game;
    Map * map;
    Input * input;
};

这是我创建对象的地方:

#include "SN.h"

SN::SN(){
    game = new Game(this);
    window = new Window(this);
    map = new Map (this);
    input = new Input (this);
}

这是我将类SN的对象指针放在例如Window.h类的指针对象中的地方:

//CONSTRUCTOR
Window::Window(SN * obj) : sn(obj){
    createWindow();
}

在Window.h中声明sn:SN * sn;

注意:我已经向前宣布了SN类。

这就是我在类Window中调用函数的方法(来自类Game.cpp):

sn->window->test();

我希望我很清楚,先谢谢。

0 个答案:

没有答案
相关问题