|
-
January 16th, 2009, 08:54 AM
#16
Re: initialize static variable in cpp file
 Originally Posted by MacDaddy C++
There can be only one!
Do you know the HighLander?? 
On a more serious note, that is the whole crux of the matter. The "line of code" which defines the static and initializes it (implicitly or explicitly) must be seen by the compiler only once for all of the translation usings that will be linked into the final program.
Typically this is best done by putting the code in a specific .cpp file. And thos post does NOT contract that, nor recommend the following approach (although it is useful in certain very specific cases...
This approach should make immediate sense to anyone who has used preprocessor directives to conditionally "dllExport" / "dllImport"...
SomeFile.h
Code:
class Foo
{
static SomeClass StaticInstance;
};
#ifdef SOME_FILE_CPP_COMPILING
Foo::StaticInstance(params);
#endof
SomeFile.cpp
Code:
#define SOME_FILE_CPP_COMPILING
#include "SomeFile.h"
This can also be accompished using the __FILE__ predefined value.
AGAIN, This is NOT recommended as a general practice. But for cases where the value of the static needs to be "published" (known to users of the header file), it provides a means that is guaranteed to remain "in sync" (A comment in the header file could easily differ from a value in an implementation file.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 16th, 2009, 10:03 AM
#17
Re: initialize static variable in cpp file
 Originally Posted by PredicateNormative
I'm not saying that you're wrong, perhaps you are right, but I can't find this in the standard, or at least not in the 2005 draft that I have. 
In the 2003 version ... Section 9.4.2 Static data members ... paragraph 4
If a static data member is of const integral or const enumeration type,
its declaration in the class definition can specify a constant-initializer
which shall be an integral constant expression (5.19). In that case, the
member can appear in integral constant expressions. The member shall still
be defined in a namespace scope if it is used in the program and the namespace
scope definition shall not contain an initializer.
-
January 16th, 2009, 03:43 PM
#18
Re: initialize static variable in cpp file
 Originally Posted by Philip Nicoletti
I was not aware of that particular bug in the Visual C++ compiler.
This is NOT a bug, this is an EXTENSION.
If you don’t like it – disable it with a compiler switch.
Following your logic, my car has a bug – it has 10 airbags, while the standard is 2 (or 4, or 6).
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
January 16th, 2009, 04:05 PM
#19
Re: initialize static variable in cpp file
 Originally Posted by VladimirF
This is NOT a bug, this is an EXTENSION.
If you don’t like it – disable it with a compiler switch.
Following your logic, my car has a bug – it has 10 airbags, while the standard is 2 (or 4, or 6).
????????????????
An incorrect link error is an extension ???
Maybe I don't understand what you did to get the link error.
Do you get the link error with the defintion in the CPP file ? If so,
it is a bug. It is not clear to me what code you used to get an
error. The code that I posted should not get a link error.
-
January 16th, 2009, 04:35 PM
#20
Re: initialize static variable in cpp file
 Originally Posted by Philip Nicoletti
An incorrect link error is an extension ???
My understanding of this extension is that it treats static const member variable declaration with initialization as definition, making another definition redundant.
Actually, sounds pretty logical to me. Unlike standard initialization with declaration that requires out-of-class definition. If it isn’t defined yet, what do you initialize???
 Originally Posted by Philip Nicoletti
Maybe I don't understand what you did to get the link error.
Do you get the link error with the defintion in the CPP file ?
No, you got it right. And I see how it can annoy people who use multiple compilers.
I might have stepped into a wrong forum
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
January 16th, 2009, 04:46 PM
#21
Re: initialize static variable in cpp file
 Originally Posted by Philip Nicoletti
In the 2003 version ... Section 9.4.2 Static data members ... paragraph 4
If a static data member is of const integral or const enumeration type,
its declaration in the class definition can specify a constant-initializer
which shall be an integral constant expression (5.19). In that case, the
member can appear in integral constant expressions. The member shall still
be defined in a namespace scope if it is used in the program and the namespace
scope definition shall not contain an initializer.
I'm not sure I follow what you are suggesting. Actually, I tried an experiment with my compiler because I thought I understood. Take a look at this.
Code:
#ifndef __FOO__
#define __FOO__
class foo
{
public:
static const int defaultValue = 10;
foo();
~foo();
void setValue(int newValue);
int getValue();
private:
int value;
};
#endif
// foo definition
#include "foo.h"
foo::foo()
{
value = foo::defaultValue;
}
foo::~foo()
{
}
void foo::setValue(int newValue)
{
value = newValue;
}
int foo::getValue()
{
return value;
}
#include "foo.h"
int main ()
{
std::cout << "foo::value" << foo::defaultValue << std::endl;
foo f;
std::cout << f.getValue() << std::endl;
f.setValue(5);
std::cout << f.getValue() << std::endl;
return 0;
}
I did not understand the point of the statement in bold. In this case, I still used to defaultValue attribute within the program and within foo's definition without having to define it in the .cpp. Can someone explain what the last sentence in bold means or provide an example that demonstrates its meaning?
I use AdaMulti v4.2.3. it is set to use std c++ without any extensions. Also, I tried defining the defaultValue param in the .cpp file in addition to the declaration and initialization in the header file and received no linker errors. My compiler seems to be happy with or without the definition in the .cpp file so long as it is initialized in the header. That seems inconsistent with the paragraph of the std posted by Philip.
Last edited by kempofighter; January 16th, 2009 at 04:58 PM.
Reason: additional info
-
April 11th, 2012, 04:53 PM
#22
Re: initialize static variable in cpp file
 Originally Posted by VladimirF
My understanding of this extension is that it treats static const member variable declaration with initialization as definition, making another definition redundant.
Actually, sounds pretty logical to me. Unlike standard initialization with declaration that requires out-of-class definition. If it isn’t defined yet, what do you initialize???
No, you got it right. And I see how it can annoy people who use multiple compilers.
I might have stepped into a wrong forum 
VladimirF
How did you disable this extension? I am running into the same problem and would like to disable it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|