[C++] Header declarations

Started by
5 comments, last by Kylotan 15 years, 9 months ago
#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")?
--"I'm not at home right now, but" = lights on, but no ones home
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.
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]
--"I'm not at home right now, but" = lights on, but no ones home
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.
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.
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.)
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.

This topic is closed to new replies.

Advertisement