CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    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 ?

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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...

  3. #3
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    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};

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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....

  5. #5
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    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.


    Quote Originally Posted by Andreas Masur
    you can do it on your own
    That's , Thanks.
    Last edited by Sahir; January 26th, 2005 at 05:27 AM.

  6. #6
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    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.

  7. #7
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    Re: Is this normally allowed in C++ or is it an MS language extension.

    Ahem!

    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.

  8. #8
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    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.

  9. #9
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    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?

  10. #10
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: Is this normally allowed in C++ or is it an MS language extension.

    Quote 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}
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  11. #11
    Join Date
    Nov 2004
    Location
    Virginia, The lovers' state
    Posts
    64

    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
    Last edited by raghuvamshi; January 26th, 2005 at 02:00 PM. Reason: adding a comment

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    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

  13. #13
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    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?

  14. #14
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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
  •  





Click Here to Expand Forum to Full Width

Featured