By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

static constant as class member

P: n/a
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?

Nov 4 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
raan wrote:
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.
--
Bill Medland
Nov 4 '06 #2

P: n/a


On Nov 4, 10:50 pm, "raan" <palaka...@gmail.comwrote:
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).
}/*********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?
Nov 4 '06 #3

P: n/a

raan wrote:
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).

Nov 4 '06 #4

P: n/a
Salt_Peter wrote:
raan wrote:
>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.
He doesn't need an instance to access a static member.

Nov 4 '06 #5

P: n/a

static const int i=0;
I know that is the right way, apparently VC6 doesn't let you do so. You
can find the exact situation, right here.

http://groups.google.com/group/alt.c...6cf5a05f97963e

Thats exactly I was talking about.

Nov 5 '06 #6

P: n/a

red floyd wrote:
Salt_Peter wrote:
raan wrote:
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.

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;
}

Nov 5 '06 #7

P: n/a
Salt_Peter wrote:
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

Nov 5 '06 #8

P: n/a

Greg wrote:
Salt_Peter wrote:
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.
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.

Nov 5 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.