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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

And a user-provided constructor.
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: 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.
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

2 Comments

Or provide a constructor that takes an std::initializer_list.
I don't think std::initializer_list can be used when you have multiple types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.