If I were to follow the RAII rule and would be developing a class in C++, would it be necessary to have static constructors? Will static constructors help me in any way or would be a wrong step to do?
Share
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.
I take it you are talking about a static factory function that creates an instance of your class (As others pointed out). In which case, you don’t need to use the RAII pattern.
Remember you need your class to be stack allocated, so that the
constructor is called (automatically) and initializes various data.
also, the
destructor is called (automatically) when the stack unwinds and performs other operations: such as freeing resources etc..
If your class initializes it’s data statically then the RAII pattern will fail, since statically held data is not bound to an instance of a class. So when the stack unwinds there is no instance to destruct, no destructor gets called, and the RAII pattern is not implemented.