CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    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

  3. #3
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314
    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    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

  5. #5
    Join Date
    Dec 2001
    Location
    Bremen, Germany
    Posts
    314
    That's good to hear
    Thank you very much!

    Oliver.

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871
    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.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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???
    ...
    }
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449
    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

  9. #9
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871
    New idea! Well, can you tell me one or more compilers that supports it?
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449
    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

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449
    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

  12. #12
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871
    What "should also support it"?? Is it language standard or not? Is any real compiler supporting it?
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449
    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

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449
    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

  15. #15
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871
    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?
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured