Jump to content
  • Advertisement
Sign in to follow this  
flammable

[c++] problem with a global instance of a class

This topic is 3203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I've a problem, I'll first explain what I try to do. Create a class and a global instance of that class that can be accessed from everywhere I include the right header file, like how std::cout works (as far as I understand). I figured that the extern keyword was needed in this case and what I've done is this:
class Test_
{
	public:
		void greet()
		{
			std::cout << "Hello World!";
		}

};

extern Test_ Test;

and that gives the following linker error (I'm using visual studio c++ 2008):
1>main.obj : error LNK2001: unresolved external symbol "class Test_ Test" (?Test@@3VTest_@@A)
1>C:\Documents and Settings\gebruiker\Bureaublad\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals

I noticed that everything linked fine when I changed the last line into this:
extern Test_ Test = Test_();

but in the real case this isn't practical, and std::cout seems to do without it (but I'm sure I'm missing something). I hope someone can help, thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
extern means: is actually instanced elsewhere. They typical extern pattern is

MyClass.h file

extern MyClass gAnInstance;




MyClass.cpp file

MyClass gAnInstance; //this is the actual instance




Remember always that the problem with global instances declared like that is unreliable destructor calling. Order of operations won't be guaranteed. If you really need a global it's often better to have the global be declared as a pointer initialized to NULL. During your main program init method block instance that pointer and in your main application shutdown method delete the instance:

Only at the top of files that need it:

extern MyClass *gAnInstance;



probably in your main.cpp file:

MyClass *gAnInstance = NULL; //this is the actual instance




void init()
{
gAnInstance = new MyClass();
}

void shutdown()
{
delete gAnInstance;
}

void main
{
init();

//your game loop
while (1)
{
}

shutdown();
}



There are a lot of other things that go into globals like designing the class so it's a singleton or whatever. but I won't bother with that here. just google "singleton"

-me

Share this post


Link to post
Share on other sites
Thanks, I see now that I misunderstood the extern concept, that explains alot.
I know what a singleton is and you're right it's probably better go for singletons.

Thanks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!