Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

10
  • "Data objects shouldn't be defined in header files at all, only declared extern if they need external linkage." This is imprecise. "Data object", as you call them, have external linkage by default, unless they're const. So extern is needed to make their declaration just a declaration and not a definition, which is the default. However if both extern and an initializer are present, it is a definition. Commented Oct 8, 2010 at 14:18
  • Data objects often need to be defined in header files. That includes constants (which are objects). I guess it's good advice for C++ novices to avoid defining (static class member) constants of non-integral types in header files. More experienced programmers can do that using e.g. the templated constants trick. Another such trick is a function returning a reference to a local static constant. Commented Oct 8, 2010 at 14:24
  • @Armen, @Alf: yes, my advice concerning data objects was a bit simplistic. I'll rephrase it. Commented Oct 8, 2010 at 14:31
  • 1
    OK, good explanation for external and static member functions. Concerning inline I always thought it would replace the function calls with the function code itself and thus I end up with a lot of copies?! Commented Oct 8, 2010 at 14:31
  • 4
    "or in an unnamed namespace." - nooo that's utterly bad. Unnamed namespaces are for .cpp files that want to make data, functions or types TU-local. So that they can use common names and are guaranteed to not conflict. But if you place them into the header, you pollute that private environment! Commented Oct 9, 2010 at 8:45