Given codes in language C++
/* file xxx.hpp */
class A
{
};
class B
{
private:
class C
{
static const A a;
};
};
How can I initialize the static constant member variable A a in nested class C?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the cpp file with code that is to be compiled you should add
const A B::C::a = {};.The initializer (be it an initializer list in {…} for POD classes or aggregate types or a single constant value for built-in types) is optional – if it’s not specified, a default constructor will be called for
a. In case of primitive types, it should be set to 0.updated:
As David has greatly remarked below, some compilers issue warnings when no initializer is specified for a static member definition. If there are some data members in
class Aand no initializer during definition ofais specified, my g++ 4.6.3 compiler issues the following warning (that is by default is treated as error):