0

I have a strange problem. I can call the function in the Window class(named test) with a pointer to an object I created, but when I cout the pointer it is null(output: 00000000). Is this even possible? That might work, but other classes are not able.

This is where I declare my pointers(normal, right?):

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

This is where I create the objects:

#include "SN.h"

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

This is where I put the object pointer of class SN in the pointer object of for example class Window.h :

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

declaring sn in Window.h: SN * sn;

Note: I already have forward declared the class SN.

This is how I call a function in the class Window(From the class Game.cpp):

sn->window->test();

I hope I was clear enough, thanks in advance.

OpenGLmaster1992
  • 281
  • 2
  • 5
  • 13
  • 4
    Undefined behavior is not guaranteed to crash your program. Anything can happen, including appearing to work. – interjay Dec 03 '15 at 15:17
  • @interjay So this is not "fixable"? – OpenGLmaster1992 Dec 03 '15 at 15:19
  • 1
    If you don't want to allow nulls, you could use references instead of pointers. – Andy M Dec 03 '15 at 15:19
  • @Andy But I was learned that pointer give more performance than references. – OpenGLmaster1992 Dec 03 '15 at 15:20
  • It's fixable by making sure you don't call a function on a null pointer. – interjay Dec 03 '15 at 15:21
  • 2
    @HugoCornel you learned wrong. – eerorika Dec 03 '15 at 15:22
  • @HugoCornel With any decent compiler, you should see no difference in performance with references instead of pointers. – Andy M Dec 03 '15 at 15:22
  • @HugoCornel Umm.. it's the opposite, if anything. Basically it is the same, references provide more assertions that can be made, so the compiler might be more agressive on occasions. – luk32 Dec 03 '15 at 15:22
  • So to fix this I have to replace actually the * with the & and call the function using . instead of -> ? – OpenGLmaster1992 Dec 03 '15 at 15:23
  • @user2079303 How can this be duplicate while I never said undefined behaviour and actually don't really know what it is? – OpenGLmaster1992 Dec 03 '15 at 15:24
  • @HugoCornel Because the answer is the same... Your question is essentially the same. You ask "is it bad", the other asks "why is it so". – luk32 Dec 03 '15 at 15:25
  • @luk32 Ah, okay can I close this question now or something? – OpenGLmaster1992 Dec 03 '15 at 15:25
  • @HugoCornel the question is about doing the same thing. The answer explains why it's undefined behaviour. Knowing that it's undefined behaviour explains why your expectations about the code are meaningless. About replacing all your pointers with references: sure, if you have no pointers at all, then no pointer can be null. It may not be possible to avoid pointers always, though. – eerorika Dec 03 '15 at 15:26

0 Answers0