George Kourtis
George Kourtis

Reputation: 2592

Class initialization parens to use

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

It seems that the syntax AAA bbb(20) isn't accepted !

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

Or I miss some point ?

Upvotes: 2

Views: 249

Answers (1)

juanchopanza
juanchopanza

Reputation: 227418

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.

Upvotes: 8

Related Questions