static constant as class member

R

raan

Microsoft C++ apparently doesn't permit me to initialize a static
constant with in my class scope. Ok, I think that is previously
discussed here and people have suggested some hacks to circumvent the
issue. That being said, when you try to use that constant as in case
expression things get wild.

Here is my implementation.

/*test.h*/
class test{
public:
static const int i;
};

/*test.cpp*/
const int test::i=0;

/*main.cpp*/
#include "test.h"
#include <iostream>
using namespace std;

int main()
{
int k = 0;

switch(k)
{
case test::i:
cout<<"the case\n";
break;
default:
break;
}
return 0;
}

/*********Error snippet ***********/
error C2051: case expression not constant
warning C4065: switch statement contains 'default' but no 'case' labels

How could I get off with it. Technically this is what C++ suggest and
is ideal, right?
 
B

Bill Medland

raan said:
Microsoft C++ apparently doesn't permit me to initialize a static
constant with in my class scope. Ok, I think that is previously
discussed here and people have suggested some hacks to circumvent the
issue. That being said, when you try to use that constant as in case
expression things get wild.

Here is my implementation.

/*test.h*/
class test{
public:
static const int i;

static const int i=0;
};

/*test.cpp*/
const int test::i=0;

No. Presumably you got some sort of complaint about this line.
/*main.cpp*/
#include "test.h"
#include <iostream>
using namespace std;

int main()
{
int k = 0;

switch(k)
{
case test::i:
cout<<"the case\n";
break;
default:
break;
}
return 0;
}

/*********Error snippet ***********/ Yes, I'll bet it's a snippet.
error C2051: case expression not constant
warning C4065: switch statement contains 'default' but no 'case' labels

How could I get off with it. Technically this is what C++ suggest and
is ideal, right?
I don't think so.
 
O

Osamede.Zhang

Microsoft C++ apparently doesn't permit me to initialize a static
constant with in my class scope. Ok, I think that is previously
discussed here and people have suggested some hacks to circumvent the
issue. That being said, when you try to use that constant as in case
expression things get wild.

Here is my implementation.

/*test.h*/
class test{
public:
static const int i;

};/*test.cpp*/
const int test::i=0;

/*main.cpp*/
#include "test.h"
#include <iostream>
using namespace std;

int main()
{
int k = 0;

switch(k)
{
case test::i:
cout<<"the case\n";
break;
default:
break;
}
return 0;

I don't know what are you talking about,everything is ok in my compiler
(vc8).
 
S

Salt_Peter

raan said:
Microsoft C++ apparently doesn't permit me to initialize a static
constant with in my class scope. Ok, I think that is previously
discussed here and people have suggested some hacks to circumvent the
issue. That being said, when you try to use that constant as in case
expression things get wild.

Here is my implementation.

/*test.h*/
class test{
public:
static const int i;
};

/*test.cpp*/
const int test::i=0;

/*main.cpp*/
#include "test.h"
#include <iostream>
using namespace std;

int main()
{
int k = 0;

switch(k)
{
case test::i:
cout<<"the case\n";
break;
default:
break;
}
return 0;
}

/*********Error snippet ***********/
error C2051: case expression not constant
warning C4065: switch statement contains 'default' but no 'case' labels

How could I get off with it. Technically this is what C++ suggest and
is ideal, right?

an instance of type test does not exist in the above code.
All you have so far is a blueprint for the test class.

/* test.h */
#ifndef TEST_H_
#define TEST_H_

class test
{
static const int i;
public:
static const int get();
};

#endif /* include guards TEST_H_ */

/* test.cpp */
#include "test.h" // <- note

const int test::i=0;

const int test::get()
{
return i;
}

/*main.cpp*/
#include <iostream>
#include "test.h"

int main()
{
test t; // the protagonist
const int k = t.get(); // so you don't have to make t a constant

switch(k)
{
case 0:
std:: cout<<"case = 0" << std::endl;
break;
case 1:
std:: cout<<"case = 1" << std::endl;
break;
default:
std:: cout<<"default case" << std::endl;
}
return 0;
}

Please not that test::get() is added above to provide a way to lock the
static variable in the case you are doing something obnoxious when
accessing it (ie: concurrency, threads).
 
R

red floyd

Salt_Peter said:
an instance of type test does not exist in the above code.
All you have so far is a blueprint for the test class.

He doesn't need an instance to access a static member.
 
S

Salt_Peter

red said:
He doesn't need an instance to access a static member.

Thats confirmed. Thanks.
Why can't test::i be used as a case constant? Out of curiosity.
Is the enum hack the only solution?

#include <iostream>

int main()
{
const int k = 0;
enum { zero, one };

switch(k)
{
case zero:
std:: cout<<"case zero" << std::endl;
break;
case one:
std:: cout<<"case one" << std::endl;
break;
default:
std:: cout<<"default case" << std::endl;
}
return 0;
}
 
G

Greg

Salt_Peter said:
Why can't test::i be used as a case constant? Out of curiosity.
Is the enum hack the only solution?

#include <iostream>

int main()
{
const int k = 0;
enum { zero, one };

switch(k)
{
case zero:
std:: cout<<"case zero" << std::endl;
break;
case one:
std:: cout<<"case one" << std::endl;
break;
default:
std:: cout<<"default case" << std::endl;
}
return 0;
}

No, because:

const int zero = 0;
const int one = 1;

would work just as well - and are are better match type-wise than the
enums for representing constant integer values.

The problem in the original program is that the declaration of the
class's const static member did not have an initializer (because the
version of the C++ compiler in use did not allow one). And unless the
const static member's declaration (and not a subsequent definition) has
an initializer, the const static member does not qualify as a constant
integral expression.

Greg
 
S

Salt_Peter

Greg said:
No, because:

const int zero = 0;
const int one = 1;

would work just as well - and are are better match type-wise than the
enums for representing constant integer values.

Yep, i realize that.
The problem in the original program is that the declaration of the
class's const static member did not have an initializer (because the
version of the C++ compiler in use did not allow one).

Yes, noticed.
And unless the
const static member's declaration (and not a subsequent definition) has
an initializer, the const static member does not qualify as a constant
integral expression.

Greg

ah! Indeed: it does work. Thank-you Greg.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top