Why are static class objects allowed in C++? what is their use?
#include<iostream>
using namespace std;
class Test {
static Test self; // works fine
/* other stuff in class*/
};
int main()
{
Test t;
getchar();
return 0;
}
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.
This just works; the compiler doesn’t have to do anything special simply because
selfis both a static member ofTestand is of typeTest. I see no reason why this special case would need to be specifically prohibited.Now, there is a problem with
Test::selfin that you declare the variable, but fail to define it. However, this is simply a bug in your code and is easily fixed: