Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I have a little problem in defining static const int in this code snippet
C++
#include <iostream>

class foo
{
public :
    static const int f = 5; 
};

void bar(const int& b)
{

}

int main()
{
    bar(foo::f); //undefined reference to foo::f
}

I have found a simple solution
C++
#include <iostream>

class foo
{
public :
    enum {f = 5};
};

void bar(const int& b)
{

}

int main()
{
    bar(foo::f); //works fine !!
}

why the first code does not work and the second one does work ? I need some explanation about what happens behind the scene, please.

Thanks, Sam.
Posted
Updated 27-Aug-15 2:39am
v2
Comments
Philippe Mori 27-Aug-15 12:38pm    
Are you using an old compiler?
Samuel Shokry 28-Aug-15 17:43pm    
I'm using code block 13.12 IDE
Afzaal Ahmad Zeeshan 27-Aug-15 20:37pm    
In my case, the above code works perfectly.
[no name] 27-Aug-15 20:48pm    
Proving what?
Afzaal Ahmad Zeeshan 27-Aug-15 20:50pm    
Proving that the member is accessible. I tried in Visual Studio.

Nothing magic here. One is permitted syntax one isn't. For const member variables must be declared in class and defined outside. http://www-01.ibm.com/support/knowledgecenter/SSLTBW_1.12.0/com.ibm.zos.r12.cbclx01/cplr038.htm[^]

So:
C#
class foo
{
public :
    static const float f;
};

const float foo:f = 5.0f;


in c++11 you could do:

C#
class foo
{
public :
    static constexpr float f = 5.0f;
};


However some C++ compilers allow the omission of an external definition for int but it is advised to have one if you intend taking the address later. This is because the compiler may remove the variable and just keep the value.
See here: http://stackoverflow.com/questions/1312241/using-a-static-const-int-in-a-struct-class[^]

This is what you discovered. You did not mention your compiler but that is crucial here.

For further explanation look here:http://stackoverflow.com/questions/11300652/static-data-member-initialization[^]

Quote
"On the other hand, a static member variable is not contained within an instance of the class, it exists independently of any single instance and exists from the start of the program, at a fixed address. In order for a static member variable (or any other global object) to get a unique address the linker must see exactly one definition of the static variable, in exactly one object file, and assign it an address.

Because a static variable needs exactly one definition in exactly one object file, it doesn't make sense to allow that definition to be provided in the class, since class definitions typically exist in header files and are included in multiple object files. So although you can provide an initializer in the class, you still need to define the static data member somewhere"

Enums do not exist in memory they are entirely handled by the compiler:
http://stackoverflow.com/questions/1206579/memory-location-of-enum-value-in-c[^]
 
Share this answer
 
v7
Comments
Samuel Shokry 27-Aug-15 10:13am    
I mean why could I take the address of a value in an enum but I could not do that for a static const int in-class initialized without an outside-class-definition ? I wish it become more clear now :)

Sam.
[no name] 27-Aug-15 19:40pm    
Const int member entirely dependant on your compiler. For the enum you are not taking address as your function is const int ref so the compiler just sticks in the const value.
Richard MacCutchan 27-Aug-15 10:36am    
I just tried it in VS2010 and it worked. Very odd.
[no name] 27-Aug-15 19:57pm    
Yes as it contradicts their own documentation:
https://msdn.microsoft.com/en-us/library/b1b5y48f(v=vs.100).aspx
Richard MacCutchan 28-Aug-15 3:17am    
I could not find a clear definition of how that is affected by the addition of the const keyword.
It is a variable which belongs to the class and not to object(instance)
Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables
A single copy to be shared by all instances of the class
A static variable can be accessed directly by the class name and doesn’t need any object!

Static variable are class level variables, they can't be declared inside a method, if declared class will not compile.

class foo
{
public :
static const int f;
};
const int foo::f = 5;

void bar(const int& b)
{

}

int main()
{
bar(foo::f);
}

Works finely and compiles without error
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900