In the code below, why does the compiler not complain for mClass2?
class CMyClass{
private:
CMyClass(){}
};
void TestMethod(){
CMyClass mClass1; //Fails.
CMyClass mClass2(); //Works.
}
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.
Because you’ve just declared a function
mClass2of zero arguments that returns aCMyClass. That’s a valid option since there could be, say, astatic CMyClassinstance which that function has access to. Note thatCMyClassstill has a public copy constructor.(To convince yourself, compile this module to assembler and observe that commenting out the line
CMyClass mClass2();produces the same output.)