Page 1 of 1
Funky static const errors.. anyone?

Posted:
Jan 21, 2004 @ 7:08pm
by wyrd
Re: Funky static const errors.. anyone?

Posted:
Jan 21, 2004 @ 7:23pm
by Kzinti
In VC 6 (and thus EVC 3 and 4), you can't declare static const member variables. The usual work around is to use enums:
class MyClass
{
enum
{
MY_CONSTANT = 100;
};
};
But then you lose type safety.
If it's uneeded in the header, then just declare it in the .cpp as a "local" static variable.

Posted:
Jan 21, 2004 @ 7:25pm
by wyrd
Ah, okay. Well at least I know I wasn't going crazy.

Thanks for the tip, enum seems like a slightly better alternative than #define (must.. not.. use.. globals).

Posted:
Jan 21, 2004 @ 7:34pm
by Pejo Software - Per
Thierry are you sure?
I think the following will work:
class CGameWindow
{
static const DWORD DEFAULT_HEIGHT;
};
In cpp file:
const DWORD CGameWindow::DEFAULT_HEIGHT=100;

Posted:
Jan 21, 2004 @ 7:45pm
by Kzinti
I don't think it does, but if it works then it not a constant anymore. It's a real variable.
I am sure though that you can't declare and initialize a static const member variable in the header file (before .NET) so that it get used as a constant.

Posted:
Jan 21, 2004 @ 7:55pm
by mlepage

Posted:
Jan 21, 2004 @ 8:00pm
by Pejo Software - Per

Posted:
Jan 21, 2004 @ 8:03pm
by Kzinti

Posted:
Jan 21, 2004 @ 8:09pm
by wyrd

Posted:
Jan 21, 2004 @ 8:16pm
by Pejo Software - Per

Posted:
Jan 21, 2004 @ 8:29pm
by wyrd