Sign in to follow this  
akaitora

static member variables in c++

Recommended Posts

In order to initialize a static member variable, why must the variable type be an integral type? Why can't we use say... floats? What's the deal here? I am using Visual Studio 2008. Thanks!

Share this post


Link to post
Share on other sites
class CBlah
{
public:

static const int iA = 1; // Works
static const float fB = 1.0f; // Doesn't Work

};


The Error:
"error C2864: 'CBlah::fB' : only static const integral data members can be initialized within a class"

Share this post


Link to post
Share on other sites
Try


class CBlah
{
public:

static const int iA = 1; // Works
static const float fB;

};

float CBlah::fB = 1.0f;




[edit]
Keyword in the error message was within -
"error C2864: 'CBlah::fB' : only static const integral data members can be initialized within a class"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this