为什么类数据成员不能通过直接初始化语法初始化? [英] Why class data members can't be initialized by direct initialization syntax?

查看:330
本文介绍了为什么类数据成员不能通过直接初始化语法初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道为什么类的数据成员不能使用()语法初始化?请考虑以下示例:

  #include< iostream& 
class test
{
public:
void fun()
{
int a(3);
std :: cout<< a<<'\\\
';
}
private:
int s(3); //编译器错误为什么?
};
int main()
{
test t;
t.fun();
return 0;
}

程序编译失败给出以下错误。

  11 9 [错误]数字常量之前的期望标识符

11 9 [错误]预期','或'...'之前的数字常量

为什么?是什么原因? C ++标准对类数据成员的初始化有什么看法?
您的帮助非常感谢。感谢

解决方案

提出功能介绍的提案解释了这是为了避免解析问题



这里只是其中一个例子:

$ b不幸的是,这会使 expression-list 在解析声明时不明确:

  S {
int i(x); //带初始化器的数据成员
// ...
static int x;
};

struct T {
int i(x); //成员函数声明
// ...
typedef int x;
};

一个可能的解决方案是依靠现有规则,如果一个声明可以是一个对象或函数,那么它是一个函数:

  struct S {
int i(j); //解析成一个成员函数,
//类型j查找但找不到
// ...
static int j;
};

类似的解决方案是应用另一个现有规则,目前仅在模板中使用,如果 T 可以是一个类型或别的东西,那么它是别的东西;如果我们真的是一个类型,我们可以使用 typename

  struct S {
int i(x); // unabmiguously a data member
int j(typename y); // unabmiguously a member function
};

这两种解决方案都引入了可能被许多用户误解的细微之处关于为什么 int i(); 在块范围不声明一个默认初始化 int



本文提出的解决方案是只允许 = em> initializer-clause 和 { initializer-list } 形式。这解决了大多数情况下的歧义问题。 [..]



I am curious to know that why class' data members can't be initialized using () syntax? Consider following example:

#include <iostream>
class test
{
    public:
        void fun()
        {
            int a(3);
            std::cout<<a<<'\n';
        }
    private:
        int s(3);    // Compiler error why???
};
int main()
{
    test t;
    t.fun();
    return 0;
}

The program fails in compilation & gives following errors.

11  9 [Error] expected identifier before numeric constant

11  9 [Error] expected ',' or '...' before numeric constant

Why? What is the reason? What the C++ standard says about initialization of class data members? Your help is greatly appreciated. Thanks

解决方案

Early proposals leading to the feature's introduction explain that this is to avoid parsing problems.

Here's just one of the examples presented therein:

Unfortunately, this makes initializers of the "( expression-list )" form ambiguous at the time that the declaration is being parsed:

struct S {
    int i(x); // data member with initializer
    // ...
    static int x;
};

struct T {
    int i(x); // member function declaration
    // ...
    typedef int x;
};

One possible solution is to rely on the existing rule that, if a declaration could be an object or a function, then it’s a function:

struct S {
    int i(j); // ill-formed...parsed as a member function,
              // type j looked up but not found
    // ...
    static int j;
};

A similar solution would be to apply another existing rule, currently used only in templates, that if T could be a type or something else, then it’s something else; and we can use "typename" if we really mean a type:

struct S {
    int i(x); // unabmiguously a data member
    int j(typename y); // unabmiguously a member function
};

Both of those solutions introduce subtleties that are likely to be misunderstood by many users (as evidenced by the many questions on comp.lang.c++ about why "int i();" at block scope doesn’t declare a default-initialized int).

The solution proposed in this paper is to allow only initializers of the "= initializer-clause" and "{ initializer-list }" forms. That solves the ambiguity problem in most cases. [..]

这篇关于为什么类数据成员不能通过直接初始化语法初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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