Thread: Magical Mystery constructor?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    596

    Magical Mystery constructor?

    Why is this array initialization legal? The array type is Part. Granted, the components inside the brackets are of the correct types for a Part, but no Part has been constructed, and there is no 2-argument constructor for Part anyway.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    const int SIZE=5;
    struct Part {
      string partNo;
      double cost;
    };
    
    int main()
    {
      Part parts[SIZE] =
        {
        {"X-101",7.50},
        {"XY-2.04",4.75}
        };
        cout << parts[0].partNo << endl;
        cout << parts[0].cost << endl;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This is ... pretty straightforward. You've always been able, from C days, to initialize a struct using an initializer list.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    I just thought, incorrectly it turns out, that C++ structs were somehow fundamentally different than C structs and required a (usually default) constructor.

    This means that the same thing can be done to instantiate a class member if all member variables are public. I've certainly never seen that before either, but it works:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    const int SIZE=5;
    class Part {
      public:
      string partNo;
      double cost;
    };
    
    int main()
    {
      Part parts[SIZE] =
        {
        {"X-101",7.50},
        {"XY-2.04",4.75}
        };
        cout << parts[0].partNo << endl;
        cout << parts[0].cost << endl;
        Part part1 = {"abc", 1.23};
        cout << part1.cost <<endl;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, as long as your main class has no constructor. struct and class are really the same thing except for whether the default is public or private.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by iMalc View Post
    Thanks. That's a great explanation.

    The really hard question is, how did you find the link to Eckel's book on the CodeGuru site?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. Parameterless Constructor - Struct
    By vb.bajpai in forum C++ Programming
    Replies: 12
    Last Post: 07-10-2007, 11:17 AM
  3. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  4. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  5. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM