Extern Const int in header file

Hello, I am trying to create a unique ID for each World_Object that is created.

1
2
3
4
5
6
7
//World_Object.h
class World_Object {
	
private:
	extern const int Object_ID;
	static int Num_Objects = 0;
}


1
2
3
4
5
6
7
//World_Object.cpp
#include "World_Object.h"

World_Object::World_Object() {
	const int Object_ID = Num_Objects;
	Num_Objects++;
}


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.

Thanks for your help
Why is Object_ID extern? It doesn't need to be.
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.
1
2
3
4
5
6
7
8
9
10
//World_Object.h
class World_Object {

private:
	const int Object_ID;
	static int Num_Objects;

public:
	World_Object();
}

1
2
3
4
5
6
7
8
9
10
//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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//World_Object.h
class World_Object {

private:
	const int Object_ID;
	static int Num_Objects;

	void initialize();

public:
	World_Object():
		Object_ID( Num_Objects++ ) {
		initialize();
	}
}

1
2
3
4
5
6
7
8
//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.

Hope this helps.
Thank you so much! I couldn't find anything online on how to do that :)
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.

1
2
3
4
//World_Object.cpp
World_Object::World_Object(int type):
Object_ID(World_Object::Num_Objects++) {}
Object_Type(type) {}



1
2
3
4
5
6
7
8
// World_Object.h
private:
	const int Object_Type;
	const int Object_ID;
	static int Num_Objects;

public:
	World_Object(int type);


it doesn't like how i'm trying to initialize both const ints in the cpp.

[EDIT] Nevermind figured it out. You have to put a "," instead of the {} after the Object_ID initializer list
Last edited on
Topic archived. No new replies allowed.