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

2 回答 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.

于 2012-09-26T07:06:51.327 回答
0

您必须使用 4 个参数声明构造函数,例如

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

}

现在你可以用大括号初始化你的变量

于 2012-09-26T07:06:09.877 回答