Search code examples
c++pointersreferenceallegro5

Reference pointer in class


Well, I need an attribute in my class is a pointer reference & and start to null, because it pointed to the address of an image that has not yet been charged. It is possible to do this?

If not possible, is as follows: I'm creating a game with Allegro 5 and C ++, and in part to carry the images of tiles (tileset) I carry every memory and for each object of the game I pass him the reference pointer (memory address) of your respective image. I'm doing the right thing? Got any better way?


Solution

  • Don't use a reference, use a pointer.

    Image* image = nullptr;
    
    image = loadImage(...);
    image->getWidth();
    

    A reference must reference an existing object, if the variable should have a "non loaded state" then you are out of luck. You must use a pointer or let it reference a non valid object created just as a placeholder.