-
May 2nd, 2004, 01:42 PM
#1
static const in switch of derived class?
Hi
This simple code below compiles without error when pased into one file. But when I split the definitions and declarations into separate files (e.g. Base.h, Base.cpp, Derived.h, Derived.cpp - just as it should be), the compiler chokes at the switch statement complaining that msc_iCatIndex, msc_iDogIndex and msc_iBirdIndex are not constants. On the other hand, if I write something like msc_iCatIndex = 15; directly before the switch statement, the compiler complains that msc_iCatIndex is a const value.
Code:
class CBase
{
public:
static const int msc_iCatIndex;
static const int msc_iDogIndex;
static const int msc_iBirdIndex;
};
const int CBase::msc_iCatIndex = 1;
const int CBase::msc_iDogIndex = 2;
const int CBase::msc_iBirdIndex = 3;
class CDerived : public CBase
{
public:
char* GetAnimalName(int iAnimal); // may also be const
};
char* CDerived::GetAnimalName(int iAnimal)
{
// compiler chokes at this line with
// error 2166 "l-value specifies const object"
// CORRECT!
//msc_iCatIndex = 15;
// the three case statements choke with
// error 2051: "case expression not constant"
// COMPILER ERROR?
switch(iAnimal)
{
case msc_iCatIndex:
return "Cat";
case msc_iDogIndex:
return "Dog";
case msc_iBirdIndex:
return "Bird";
default:
break;
}
return "unknown";
}
Is this a compiler bug? How can I bypass this without putting all the code into one file?
Thank you in advance
Oliver.
-
May 2nd, 2004, 04:08 PM
#2
1) What version of the VC++ compiler are you using?
If you are using VC 6.0, use enums. This is a bug in Visual C++ 6.0. There are actually two bugs, the other is that you are allowed to initialize static const items in the class, but Visual C++ 6.0 doesn't allow it.
Code:
class CBase
{
public:
enum {msc_iCatIndex=1,
msc_iDogIndex,
msc_iBirdIndex};
};
Regards,
Paul McKenzie
-
May 3rd, 2004, 10:57 AM
#3
Dear Paul McKenzie
Thank you for this information! I use VC6.0 with the brand-new service pack (I do not remember the number).
Using enums would be a solution, but may I ask one more question about using enums: Is it safe to assume that enums are int? Will this be safe to assume in future compiler versions? Or should I cast the enum to int everytime I assign or pass an enum to/as an int?
Thank you
Oliver.
-
May 3rd, 2004, 11:50 AM
#4
Originally posted by Oliver Twesten
Dear Paul McKenzie
Thank you for this information! I use VC6.0 with the brand-new service pack (I do not remember the number).
Using enums would be a solution, but may I ask one more question about using enums: Is it safe to assume that enums are int?
Yes, enums are integral types. There is no need to cast an enum to an int (if you have the ANSI spec, see section 7.2.5 and 7.2.8 that discusses enum to integer promotion).
Regards,
Paul McKenzie
-
May 3rd, 2004, 01:40 PM
#5
That's good to hear
Thank you very much!
Oliver.
-
June 1st, 2004, 07:12 AM
#6
Originally posted by Paul McKenzie
...
If you are using VC 6.0, use enums. This is a bug in Visual C++ 6.0. There are actually two bugs, the other is that you are allowed to initialize static const items in the class, but Visual C++ 6.0 doesn't allow it.
Nope! There is no bug like that. You can initialize ONLY integral constants within the class itself.
-
June 1st, 2004, 07:16 AM
#7
Re: static const in switch of derived class?
Originally posted by Oliver Twesten
.......
// error 2051: "case expression not constant"
// COMPILER ERROR?
switch(iAnimal)
{
case msc_iCatIndex:
....
For compiler it is error because you CAN modify constant value (any type) using typecasting.
Code:
const int msc_iCatIndex=40;
...
*((int*)&msc_iCatIndex) = 550; // This is valid and will change value!!!
For this reason compiler cannot generate code.
Another simple situation is: What if you get a 'const int' argument' value in a function and use it in switch statement?
Code:
void foo(const int n)
{
...
switch(...)
{
case n: // Will it be valid???
...
}
-
June 1st, 2004, 08:03 AM
#8
Originally posted by Ajay Vijay
Nope! There is no bug like that. You can initialize ONLY integral constants within the class itself.
Not true. In C++, const static members are allowable types to be initialized in the class. It's just that Visual C++ 6.0 doesn't allow it.
Regards,
Paul McKenzie
-
June 1st, 2004, 08:07 AM
#9
New idea! Well, can you tell me one or more compilers that supports it?
-
June 1st, 2004, 08:07 AM
#10
From Comeau C++:
Code:
class CBase
{
public:
static const int msc_iCatIndex = 1;
static const int msc_iDogIndex = 2;
static const int msc_iBirdIndex = 3;
};
class CDerived : public CBase
{
};
Your Comeau C/C++ test results are as follows:
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++
In strict mode, with -tused, Compile succeeded...
Regards,
Paul McKenzie
-
June 1st, 2004, 08:09 AM
#11
Originally posted by Ajay Vijay
New idea! Well, can you tell me one or more compilers that supports it?
Try Visual C++ 7.x. I believe it's fixed there. MingW, Dev C++, (both are really gcc) should also support it.
Regards,
Paul McKenzie
-
June 1st, 2004, 08:14 AM
#12
What "should also support it"?? Is it language standard or not? Is any real compiler supporting it?
-
June 1st, 2004, 08:16 AM
#13
Yes, it is a language standard, just like member template functions (which are also not supported by Visual C++ 6.0).
The bottom line is that Visual C++ 6.0 is not a good compiler to use to determine if some non-trivial C++ code is valid or not.
Regards,
Paul McKenzie
-
June 1st, 2004, 08:24 AM
#14
From the ANSI/ISO C++ Standard (9.4.2.4)
If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant initializer which shall be an integral constant expression.
Regards,
Paul McKenzie
-
June 1st, 2004, 08:28 AM
#15
So what did I say? I said ONLY integral constants can be initialized. Further I am not sure about vc6, I have been using vc7 for last 2+ years.
Further, could be give me a link for ANSI standards?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|