473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Field initializers allowed?

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
Nov 17 '05 #1
1 1237
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
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ric Has | last post by:
I have the following code which won't compile: typedef myType1 { unsigned short v1, unsigned char v2, unsigned char v3 } enum signals { SIG_A,
5
by: Unforgiven | last post by:
Me and some other people have been involved in a debate whether or not the following code is legal C++: #include <vector> struct s_stuff { std::string s_search; std::string s_url; }; int...
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
4
by: John Devereux | last post by:
Hi, gcc has started warning about the lack of inner braces in initializers like :- struct io_descriptor { int number; char* description;
3
by: Dave | last post by:
Hi everyone, Is it possible, using an Attribute or by some other means, to notify the C# Compiler to serialize all static field's that have initializers before code in an explicit static...
6
by: Rene | last post by:
Could anyone tell me what is the reason I can't initialize the members of a struct like this: public struct SomeStruct { public int uno = 1; // Error public int dos = 2; // Error public...
2
by: cody | last post by:
class Test { IList list = new ArrayList(); MyCollection list2 = new MyCollection (list); } Leads to this error. I know I could initialize them in the ctor but I'm asking myself where this...
10
by: GeezerButler | last post by:
We have 2 classes. Class B derives from A //Has 2 constructors public class A { A() { //do something } A(string name)
1
by: r035198x | last post by:
Inspiration Inspired by a post by Jos in the Java forum, I have put together a little article on class initializers. Initializers are indeed underutilized by most Java programmers. Once the Java...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.