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 ?
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... ;)
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};
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... :cool:
However....even without trying...it will result in the same error.... ;)
Re: Is this normally allowed in C++ or is it an MS language extension.
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.
I have no objection to this sort of thing, but when it's dished out without telling you this isn't standard C++, you begin to think your memory is playing tricks on you. :mad: :sick: :sick:
Quote:
Originally Posted by Andreas Masur
you can do it on your own
That's :cool: , Thanks.
Re: Is this normally allowed in C++ or is it an MS language extension.
Quote:
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.
Re: Is this normally allowed in C++ or is it an MS language extension.
Ahem! :o :o
Well at least my memory isn't playing tricks on me. I did not know
Code:
#include <iostream>
using namespace std;
struct A{
int s;
};
struct B{
A a;
int x;
char* c;
};
int main(int argc, char* argv[]){
B b = {10};
cout<<b.x<<endl;
return 0;
}
was legal. Interesting. I wonder which member gets set to 10. :D
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;
}
:rolleyes:
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"
Re: Is this normally allowed in C++ or is it an MS language extension.
Quote:
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? ;)
Re: Is this normally allowed in C++ or is it an MS language extension.
Quote:
Originally Posted by Sahir
Ahem! :o :o
I wonder which member gets set to 10. :D
The variables are pass to the object in the order the object list the data members.
So 10 would go to {a}
Re: Is this normally allowed in C++ or is it an MS language extension.
Quote:
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
Re: Is this normally allowed in C++ or is it an MS language extension.
Quote:
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
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?
Re: Is this normally allowed in C++ or is it an MS language extension.