Field initializers allowed?

G

Guest

I have Visual Studio 7.1 and I wrote the following C++ program

#include <iostream
using namespace std

class ID
private
static unsigned long s_id
public
static unsigned long Next(

return (++s_id)

}
unsigned long ID::s_id = 0

class A
public
static const int id = ID::Next()
}

class B
public
static const int id = ID::Next()
}

int main(int argc, char* argv[]

cout << "A.id=" << A::id << endl
cout << "B.id=" << B::id << endl

return 0


I believe this is incorrect C++ and shouldn't compile. However, I get zero compiler errors and a program output of

A.id=
B.id=

When I compiled this on GCC 2.95.3 (March 2001) I get the following errors at the two id=ID::Next() lines

"field initializer is not constant

Shouldn't VC give me the same errors? Why is it compiling to produce '0'

Thanks
Jeff
 
H

Holger Grund

Jeff,
class A {
public:
static const int id = ID::Next();
};
Seems to be a bug. The initializer in the declaration must be
a constant integral expression.
A.id=0
B.id=0
Seems that VC simple ignores the initializer and hence does not perform
dynamic initialization.
Shouldn't VC give me the same errors? Why is it compiling to produce '0'?
That's indeed odd. FWIW it seems to be fixed with current Whidbey alpha.

-hg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top