1

I am going through this question:Aggregates and pods When an object of class in C++ with user defined default constructor, has only some of it's data members initialized,will the rest of data members be value initialized? Following is the program I tried resulting in compilation error:

#include <iostream>
using namespace std;

class A {
public:
    A() {
        i=10;
        f = 10.0f;
        c = 45;
        d = 10.0;
    }

    void show() {
        cout << i << "\t" << f << "\t" << c << "\t" << d<<"\n";
    }

private:
    int i;
    float f;
    char c;
    double d;
};

int main() {
    A a={20,20.0f};
    a.show();
}
Community
  • 1
  • 1
ProgEnthu
  • 443
  • 1
  • 5
  • 9

2 Answers2

3

Your class does not qualify as an Aggregate because it has private non-static data members.

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal-initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

EDIT:

For objects of non aggregate classes if only some of the data members are initialized, will the rest be value initialized(assigned with 0)?

The rules are specified in:

C++11 8.5.4 List-initialization [dcl.init.list] Para 3:

List-initialization of an object or reference of type T is defined as follows:
— If the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized.
— Otherwise, if T is an aggregate, aggregate initialization is performed (8.5.1).
— Otherwise, if T is a specialization of std::initializer_list, an initializer_list object is constructed as described below and used to initialize the object according to the rules for initialization of an object from a class of the same type (8.5).
— Otherwise, if T is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution (13.3, 13.3.1.7). If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed.
— Otherwise, if T is a reference type, a prvalue temporary of the type referenced by T is list-initialized, and the reference is bound to that temporary. [ Note: As usual, the binding will fail and the program is ill-formed if the reference type is an lvalue reference to a non-const type. —end note ]
— Otherwise, if the initializer list has a single element, the object or reference is initialized from that element; if a narrowing conversion (see below) is required to convert the element to T, the program is ill-formed.
— Otherwise, if the initializer list has no elements, the object is value-initialized.
— Otherwise, the program is ill-formed.

Your program falls in none of the scenarios mentioned and hence falls under the the ill formed case.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • And a user-provided constructor. – juanchopanza Sep 26 '12 at 07:13
  • Yes I agree,my doubt is for objects of non aggregate classes if only some of the data members are initialized ,will the rest be value initialized(assigned with 0)? – ProgEnthu Sep 26 '12 at 07:14
  • @ProgEnthu: Updated the answer with details.You might want to see the relevant section of the standard since it shows more code examples.I only added the quotes not the examples in here. – Alok Save Sep 26 '12 at 07:45
0

You have to declare constructor with 4 arguments like

A(int i = 10, float f = 10.0f, int c = 45, float d = 10.0f):
i(i), f(f), c(c), d(d)
{

}

Now you can initialize your a variable with braces

Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44