473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

static constant as class member

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
8 2841
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


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

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
9
by: cppsks | last post by:
Taking the address of a static const resulted in a unresolved symbol. Why is that? Is the address assigned at load time? Thanks.
14
by: John Ratliff | last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave, it's a standard C++ question, I promise. This program will compile and link fine under mingw/g++ 3.4.2, but fails to link...
5
by: Martin Vorbrodt | last post by:
so if i have this: // header.h class X { public: static int s = 1; }; i still need this:
10
by: stonny | last post by:
I read the following sentence from a c++ website, but can not understand why. can anyone help me with it? " An important detail to keep in mind when debugging or implementing a program using a...
18
by: mati | last post by:
Hi The following code works: #include <vector> class C { private: static const int m_static = 2; public: void f(const std::vector<int>& v)
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.