-1

I need to make a pointer to an object of a class. However, it is declared as nullptr first. I need to make it point to that class.

Here, I declare them as 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;
};

Here I try to assign them to their object:

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

I pass the object of SN into their constructor, so they can use SN.h too. Please help me, thanks in advance.

OpenGLmaster1992
  • 281
  • 2
  • 5
  • 13
  • 3
    Pointers do not point to classes. Pointers point to objects. It seems like you don't actually understand what you're trying to do... – user253751 Dec 02 '15 at 21:40
  • @immibis Okay, but is it even possible to do what I want to do? – OpenGLmaster1992 Dec 02 '15 at 21:46
  • @HugoCornel I don't know what you want to do. You said something about making a pointer point to a class, which is impossible and nonsensical. You also said something about making a pointer to an object - which object? – user253751 Dec 02 '15 at 21:49
  • Maybe you meant `game = new Game(this);` although hard to say. (and this would be a bad design) – M.M Dec 02 '15 at 21:50
  • @immibis I need for example window to point to the window object of the class named Window. How to do that? – OpenGLmaster1992 Dec 02 '15 at 21:50
  • You are creating a new game,window,map,input variable in your constructor there. If you are trying to set the member variables of your SN class then you dont want to declare new ones. So as opposed to Game *game(this) you probably want something more like this->game = new Game(this) – KorreyD Dec 02 '15 at 21:52
  • @HugoCornel Do you want to *create* an instance of `Window` and make `window` point to it? – user253751 Dec 02 '15 at 21:54
  • @immibis Yes, actually. So SN class can use window members and Window class can use SN members. – OpenGLmaster1992 Dec 02 '15 at 21:59

1 Answers1

2

Did you mean this?

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

Note: objects created with new will never be destroyed automatically; if you want them to be destroyed you have to use delete.

SN::~SN(){
    delete game;
    delete window;
    delete map;
    delete input;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user253751
  • 57,427
  • 7
  • 48
  • 90