为什么C ++ 11不支持指定的初始化列表为C99? [英] Why does C++11 not support designated initializer list as C99?

查看:560
本文介绍了为什么C ++ 11不支持指定的初始化列表为C99?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct Person
{   
    int height;
    int weight;
    int age;
};

int main()
{
    Person p { .age = 18 };
}

上面的code为C99合法的,但在C ++ 11不合法的。

The code above is legal in C99 but not legal in C++11.

那是C ++ 11不支持这样一个方便的功能的理由?

推荐答案

C ++有构造函数。如果是有意义的初始化只是一个成员,那么,可以通过实施适当的构造函数在程序pssed前$ P $。这是哪门子的抽象C ++的提升。

C++ has constructors. If it makes sense to initialize just one member then that can be expressed in the program by implementing an appropriate constructor. This is the sort of abstraction C++ promotes.

在另一方面,指定初始化功能更多的是暴露,使成员容易直接访问客户端code。这导致了事情就像一个人的年龄18(岁?),但与零的身高和体重。

On the other hand the designated initializers feature is more about exposing and making members easy to access directly in client code. This leads to things like having a person of age 18 (years?) but with height and weight of zero.

在换句话说,指定的初始化程序支持的编程风格,其中内部暴露,并在客户端被赋予的灵活性,以决定如何使用类型。

In other words, designated initializers support a programming style where internals are exposed, and the client is given flexibility to decide how they want to use the type.

C ++更感兴趣的是把灵活的一边在设计师一个类型,而不是,因此设计人员可以很容易地正确使用类型和困难使用不正确。把设计师的类型如何被初始化控制是这一部分:设计人员决定构造函数,在类初始化等

C++ is more interested in putting the flexibility on the side of the designer of a type instead, so designers can make it easy to use a type correctly and difficult to use incorrectly. Putting the designer in control of how a type can be initialized is part of this: the designer determines constructors, in-class initializers, etc.

这篇关于为什么C ++ 11不支持指定的初始化列表为C99?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆