类初始化括号使用 [英] Class initialization parens to use

查看:133
本文介绍了类初始化括号使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想初始化一些类成员,并得到错误
期望的参数声明符(使用clang ++)
同时使用g ++ 数字常量之前的期望标识符。
所以我再次读取类初始化,我写下面的代码:

I wanted to initialize some class members and I get error "expected parameter declarator" ( using clang++ ) while using g++ "expected identifier before numeric constant". So I read again the class initializations and I write the code bellow:

#include <stdio.h>    
class AAA{
public:
    int l;
    AAA(int i){l=i;}
};

class BBB{
    bool normal;
    AAA aaa=10;
    AAA bbb(20);
    AAA ccc{30};
    AAA ddd={45};
};

int main(int argc, char **argv){printf("hello world\n");return 0;}

似乎语法 AAA bbb(20)不被接受!

这是正常吗? (我使用选项-std = c ++ 11)。

Is this normal ? ( I use option -std=c++11 ).

还是我想念一点?

推荐答案

这是正常现象。 C ++ 11不允许在非静态数据成员的就地初始化中使用()括号。这是为了避免潜在的解析作为函数。您可以使用() perantheses:

This is normal. C++11 does not allow for () brackets in in-place initialization of non-static data members. This is to avoid potential parses as functions. You could use the () perantheses with this syntax:

AAA bbb = AAA(20);

,因为此表单无法解析为函数。

because this form could not be parsed as a function.

这篇关于类初始化括号使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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