|
-
January 26th, 2005, 01:13 AM
#1
Is this normally allowed in C++ or is it an MS language extension.
This is allowed.
Code:
class A{ };
int main(int argc, char* argv[]){
A a = {1, 2, 3 , 4};
return 0;
}
Doesn't seem logical. Is this mentioned in the language specifications ?
-
January 26th, 2005, 03:23 AM
#2
Re: Is this normally allowed in C++ or is it an MS language extension.
Well....the comeau compiler says the following:
Code:
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++
"ComeauTest.c", line 4: error: too many initializer values
A a = {1, 2, 3 , 4};
^
"ComeauTest.c", line 4: warning: variable "a" was declared but never referenced
A a = {1, 2, 3 , 4};
So, I guess this answers your question...
-
January 26th, 2005, 03:34 AM
#3
Re: Is this normally allowed in C++ or is it an MS language extension.
Thanks Andreas. Since you have the Comeau compiler handy could you please try one more thing.
A a = {0};
-
January 26th, 2005, 04:21 AM
#4
Re: Is this normally allowed in C++ or is it an MS language extension.
Yes...I can....but you can do it on your own... 
However....even without trying...it will result in the same error....
-
January 26th, 2005, 05:19 AM
#5
Re: Is this normally allowed in C++ or is it an MS language extension.
Last edited by Sahir; January 26th, 2005 at 05:27 AM.
-
January 26th, 2005, 06:06 AM
#6
Re: Is this normally allowed in C++ or is it an MS language extension.
 Originally Posted by Sahir
You see a lot of this in MSDN. In some situations if you dont initialize a VARIANT in this manner (i.e. VARIANT v = {0}; ). Things don't work as you expect them to.
Well, that's obvious: VARIANT is a struct, so you can initialize the members of a VARIANT instance with an initializer list. The same can be done with the public members of a class, it's pretty much standard C++. However, the code sample you provided used a class without any members, that's why the comeau compiler complained.
-
January 26th, 2005, 11:57 AM
#7
Re: Is this normally allowed in C++ or is it an MS language extension.
-
January 26th, 2005, 12:44 PM
#8
Re: Is this normally allowed in C++ or is it an MS language extension.
None of the C++ books I read mention that you could do this.
Code:
#include <iostream>
using namespace std;
struct A {
int t;
};
struct B{
A a;
int x;
int y;
char* c;
};
int main(int argc, char* argv[]){
B b = {10 , 20 , 30 , "hello world"};
cout<<b.a.t<<endl;
cout<<b.x<<endl;
cout<<b.c<<endl;
return 0;
}
You can't blame me for thinking it's a language extension. One is apt to jump to conclusions and say "aha! language extension!!" when it's from Microsoft"
Last edited by Sahir; January 26th, 2005 at 12:49 PM.
-
January 26th, 2005, 12:50 PM
#9
Re: Is this normally allowed in C++ or is it an MS language extension.
 Originally Posted by Sahir
None of the C++ books I read mention that you could do this.
Strange... it's pretty much the standard way of inizializing struct members. Maybe you've got the wrong books?
-
January 26th, 2005, 01:25 PM
#10
Re: Is this normally allowed in C++ or is it an MS language extension.
 Originally Posted by Sahir
Ahem!
I wonder which member gets set to 10. 
The variables are pass to the object in the order the object list the data members.
So 10 would go to {a}
-
January 26th, 2005, 01:58 PM
#11
Re: Is this normally allowed in C++ or is it an MS language extension.
 Originally Posted by Sahir
You see a lot of this in MSDN. In some situations if you dont initialize a VARIANT in this manner (i.e. VARIANT v = {0}; ). Things don't work as you expect them to.
.
VARIANT v = {0}; There is nothing non-standard about this. this is perfect, legal C++.
You should check out the c++ standard, at least on topics like "Initializers" ...
here is one place u can get it from (this is the larch project, but will lead you to all relevant c++ standards definitions):
http://www.cs.iastate.edu/~leavens/l.../lcpp_toc.html
-Vinayak
Last edited by raghuvamshi; January 26th, 2005 at 02:00 PM.
Reason: adding a comment
-
January 26th, 2005, 02:38 PM
#12
Re: Is this normally allowed in C++ or is it an MS language extension.
 Originally Posted by Sahir
None of the C++ books I read mention that you could do this.
The only "book" that mentions basically everything you can do is the ANSI C++ language specification.
No book (unless it is stated that it contains the language spec.) can cover every single different way to initialize an object, let alone the other aspects of the language.
Regards,
Paul McKenzie
-
January 26th, 2005, 09:21 PM
#13
Re: Is this normally allowed in C++ or is it an MS language extension.
I need a book like the ANSI C++ language specification. After all, at least half of my questions are on topics that could easily be solved with a book like that. From what I've heard there are multiple C++ language specifications such as ISO (I thingk). Which one is the newest? Do compiler writers use all of them? Finally, are the books easily obtainable or outrageously over priced?
-
January 27th, 2005, 04:07 AM
#14
Re: Is this normally allowed in C++ or is it an MS language extension.
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
|