initialization list

Discussion in 'C++' started by giuseppe.on.usenet, Sep 14, 2011.

  1. class MyClass {
    public:
    MyClass(int a = 0) : var(a) { };
    private:
    int var;
    };

    int main() {
    /* why can I use the initialization list here? */
    MyClass myArray[2] = {1, 2};
    }


    Obviously int myArray[2] = {1, 2} would have been correct, but I don't
    understand why MyClass myArray[2] is correct as well.
    giuseppe.on.usenet, Sep 14, 2011
    #1
    1. Advertising

  2. giuseppe.on.usenet <> wrote:
    > Obviously int myArray[2] = {1, 2} would have been correct, but I don't
    > understand why MyClass myArray[2] is correct as well.


    Why shouldn't it be correct? If you want to get an error, try making
    the constructor explicit.
    Juha Nieminen, Sep 14, 2011
    #2
    1. Advertising

  3. On Sep 14, 3:33 am, "giuseppe.on.usenet"
    <> wrote:
    > class MyClass {
    > public:
    >     MyClass(int a = 0) : var(a) { };
    > private:
    >     int var;
    >
    > };
    >
    > int main() {
    >     /* why can I use the initialization list here? */
    >     MyClass myArray[2] = {1, 2};
    >
    > }
    >
    > Obviously int myArray[2] = {1, 2} would have been correct, but I don't
    > understand why MyClass myArray[2] is correct as well.


    Hi

    I believe it's OK.
    you have a similar case like this:
    MyClass c = 1; // convert int to MyClass.
    // compiler implicitly call conversion constructor
    Now, we have initializer list of integers. So for each initializer,
    compiler calls the conversion constructor.

    HTH,
    -- Saeed Amrollahi
    Saeed Amrollahi, Sep 14, 2011
    #3
  4. On 9/13/2011 5:33 PM, giuseppe.on.usenet wrote:
    > class MyClass {
    > public:
    > MyClass(int a = 0) : var(a) { };
    > private:
    > int var;
    > };
    >
    > int main() {
    > /* why can I use the initialization list here? */
    > MyClass myArray[2] = {1, 2};
    > }
    >
    >
    > Obviously int myArray[2] = {1, 2} would have been correct, but I don't
    > understand why MyClass myArray[2] is correct as well.


    It is correct because the language specification says it is correct. Your

    MyClass myArray[2] = {1, 2};

    is interpreted as

    MyClass myArray[2] = { MyClass(1), MyClass(2) };

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Sep 14, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    926
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    431
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    451
    Ron Natalie
    Dec 11, 2005
  4. toton
    Replies:
    5
    Views:
    920
    Victor Bazarov
    Sep 28, 2006
  5. aaragon
    Replies:
    2
    Views:
    606
    James Kanze
    Nov 2, 2008
Loading...

Share This Page