There are two problems... one being that the compiler won't let me initialize the static int in the header file (is there any other way than just initializing it with a boolean flag in an if statement in the constructor?) and I keep getting an uninitialized member World_Object::Object_ID and a warning for an unused variable Object_ID.
The Object_ID in the header and the cpp don't seem to be linking correctly and I can't figure out why.
Because I need to use it throughout the World_Object.cpp file in some additional methods, so I figured having it in the header file would be the best place to do that, if it's not extern the compiler needs it to be initialized right away in the header file, which I can't do until the constructor.
Actually initializing the static int in a if statement in the header wouldn't work either, as the boolean flag would have to be static too. This is quite frustrating, and I'm sure there has to be an easier way around this.
//World_Object.cpp
#include "World_Object.h"
int World_Object::Num_Objects = 0;
World_Object::World_Object:
//member fields should be initialized with initialization lists
Object_ID( World_Object::Num_Objects++ ) {
//any other special processing you want to do on construction goes here.
}
Personally, I prefer to offload initializations to another function:
//World_Object.cpp
#include "World_Object.h"
int World_Object::Num_Objects = 0;
void World_Object::initialize() {
//any special processing you want to do on construction goes here.
}
That way, things like assignment and the like are more easily implemented...
Notice also how the cpp file contains the initialization for the static field.
How would I use an initialization list to initialize two const ints? I now have another const int called Object_Type, which I wish to set to an int passed to the World_Object constructor.