1

Here there is a class with two private fields x and y;

class Point
{
private:
    int x, y;
public:
    Point(int = 1,int = 1);
    void move(int, int);
    void print()
    {
        cout << "X = " << x << ", Y = " << y << endl;
    }
};

When initialization the array of Point objects as below, the output is ok;

Point array1[] = { (10), (20), { 30, 40 } };

Output;

First array
X = 10, Y = 1
X = 20, Y = 1
X = 30, Y = 40

However, if we initilize the Point array as below, the output is weird;

Point array2[] = { (10), (20), (30, 40) }; 

Output;

Second array
X = 10, Y = 1
X = 20, Y = 1
X = 40, Y = 1

Why (30,40) is not working for initialization of Point object?

Here is the complete test code;

#include <iostream>
using namespace std;

class Point
{
private:
    int x, y;
public:
    Point(int = 1,int = 1);
    void move(int, int);
    void print()
    {
        cout << "X = " << x << ", Y = " << y << endl;
    }
};

Point::Point(int x, int y)
{
    cout << "..::Two Parameter Constructor is invoked::..\n";
    this->x = x;
    this->y = y;
}

void Point::move(int x, int y)
{
    this->x = x;
    this->y = y;
}

int main()
{
    // Point array1[] = { Point(10), Point(20), Point(30, 40) };
    // Use parenthesis for object array initialization;
    Point array1[] = { (10), (20), { 30, 40 } };    // curly bracket used for two parameter
    Point array2[] = { (10), (20), (30, 40) };      // paranthesis used for all objects

    cout << "First array" << endl;
    for (int i = 0; i < 3; i++)
        array1[i].print();

    cout << "Second array" << endl;
    for (int i = 0; i < 3; i++)
        array2[i].print();

    return 0;
}

And the complete output of test code;

..::Two Parameter Constructor is invoked::..
..::Two Parameter Constructor is invoked::..
..::Two Parameter Constructor is invoked::..
..::Two Parameter Constructor is invoked::..
..::Two Parameter Constructor is invoked::..
..::Two Parameter Constructor is invoked::..
First array
X = 10, Y = 1
X = 20, Y = 1
X = 30, Y = 40
Second array
X = 10, Y = 1
X = 20, Y = 1
X = 40, Y = 1
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106

3 Answers3

5

Why (30, 40) is not working:

stating (30, 40) is not the same as stating {30, 40} nor stating (30) is the same with {30}.

(30, 40) is a sequence of expressions (in this case integral literals), separated by comma operator, which evaluates to the last expression (i.e., 40). Whereas, {30, 40} in the context used is an aggregate initialization list.

101010
  • 41,839
  • 11
  • 94
  • 168
5

The compiler takes (30, 40) as an expression with a comma operator that evaluates to the single number 40. You should turn on compiler warnings to find that the 30 is discarded.

Parenthesized expressions in array initializers are taken as expressions, not constructor invocations. You can call the constructor explicitly to remove ambiguity.

owacoder
  • 4,815
  • 20
  • 47
4

The parenathese in your code are causing you confusion. When you write (10) this does not mean call a constructor with a parameter of 10. (10) becomes 10 and you can se that with

Point array1[] = { 10, 20, { 30, 40 } };

So for the second array

(30, 40)

Uses the comma operator so

 { 10, 20, (30, 40) }

Becomes

 { 10, 20, 40 }

If you want to call the two parameter constrcutor you will have to bracket it like the first example or explicitly call the constructor

{ 10, 20, Point(30, 40) }
NathanOliver
  • 171,901
  • 28
  • 288
  • 402