Jump to content
  • Advertisement
Sign in to follow this  
AngleWyrm

[C++] Header declarations

This topic is 3625 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

#ifndef HEADER_HPP
#define HEADER_HPP

class foo
{
  // ...
} var; // Using this generates multiple definitions

extern foo var; // Using this generates undefined reference

#endif



The first var tries to get created every include. The second var doesn't get created anywhere. How can I get it created just the one time? Is there a way to do this without creating a second file ("header.cpp")?

Share this post


Link to post
Share on other sites
Advertisement
Read this article. Please note that global variables like the one you are proposing are frowned upon, as they typically are indicative of poor design.

Finally, while class foo { } var; is valid C++, it isn't really idiomatic C++. If you are using global variables, I would use the extern foo var; option, as it is clearer in its intent.

Share this post


Link to post
Share on other sites
If by "frowned upon" you mean that there is currently a religeous campaign going on with lots of flag waving and sword rattling, then yes, I know all about it.

Let's just pretend I'm making a templated library.

The extern reference seems to only work if I force the user to instantiate a copy within his code, which isn't really a self-contained library. I don't want to burden the user with instancing my defaults.

Edit: Wanted to add a further note on the "Dont do globals" campaign: First, I generally agree with it, but second and more importantly, there must be a replacement in order for improvement to take place. The 'goto' statement was replaced by function calls; we can look back at the spaghetti code that gotos spawned and laugh, but until function calls were implemented there was nothing else to go to.

In other words, it's my opinion that an article stating "globals are bad" would be much more productive if it said "In this situation, method-X works better than using a global."

[Edited by - AngleWyrm on July 20, 2008 12:41:05 AM]

Share this post


Link to post
Share on other sites
I would suggest not using global variables (because of the link issues they create) and instead use functions returning references to static local variables. You can also go for a static member variable of a template class, if necessary.

Share this post


Link to post
Share on other sites
Well, unless I misread your code, the only global you have there is "std_rand". Instead of making that global, you could rewrite the line "hat( random_type rng_instance = std_rand )" to read "hat( random_type rng_instance = rng_instance() )", which I believe would work fine.

Share this post


Link to post
Share on other sites
Quote:
Original post by AngleWyrm
Is there a way to do this without creating a second file ("header.cpp")?

What's wrong with that? (Besides the horrible name.)

Share this post


Link to post
Share on other sites
Quote:
Original post by AngleWyrm
Let's just pretend I'm making a templated library.

The extern reference seems to only work if I force the user to instantiate a copy within his code, which isn't really a self-contained library. I don't want to burden the user with instancing my defaults.


Then you're out of luck. The compiler won't know how much storage to allocate when nothing has told it what type to use as a parameter yet.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!