Thread: Initializing constant object member of a class

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Initializing constant object member of a class

    I've got a class Pacman declared as follows:
    Code:
    #ifndef PACMAN_H
    #define PACMAN_H
    
    #include "Character.h"
    #include "Map.h"
    
    class Pacman : public Character
    {
    public:
    	static const double STARTING_XPOSITION;
    	static const double STARTING_YPOSITION;
    	static const Tile STARTING_TILE;
    
    	Pacman();
    	Pacman(std::string name, Position position, int state = ACTIVE,
    	int direction = LEFT, double speed = NORMAL, int pillsTaken = 0);
    ...
    In the implementation file, Pacman.cpp, I have

    Code:
    const Tile Pacman::STARTING_TILE = Map::tiledMap[10][11];
    tiledMap is simply a static 2d array of Tile objects.
    If I make the Pacman constructor print out the data of Map::tiledMap[10][11] it does it correctly so it clearly has access, but instead of STARTING_TILE being assigned the values of tiledMap[10][11] (which is a Tile), it just has the values given by the default constructor for Tile.
    I haven't defined any assignment operators so the compiler is just using the default which is what I want... So why isn't STARTING_TILE being assigned the data of tiledMap[10][11]?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Perhaps STARTING_TILE is being constructed before tileMap's members are initialized.

    gg

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps it also doesn't make sense to make these static. You'll probably not create more than one instance, so there's no gain in making the object smaller, but should you want to make a multiplayer (more than one PacMan) then probably you won't want them to start at the same position. Or perhaps you want different maps, etc. Therefore I would rather pass this data to the constructor.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Codeplug View Post
    Perhaps STARTING_TILE is being constructed before tileMap's members are initialized.

    gg
    The "gg" always seems that you are being sarcastic with the person. It always takes me some seconds to realize that it is actually something like a signature

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  3. Passing class member function to pthread_create
    By lehe in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2009, 07:47 PM
  4. Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM