I have a class hierarchy where every class has a method compute
that will trigger some (re-) computation. This method is a virtual method (and it's pure virtual in the base class). In some cases I would like to call this method from the constructor to immediately set up my instance.
Of course this can be dangerous. Eclipse CDT Code Analysis issues the error "Calling a virtual method in constructor can cause crashes and unexpected behavior". I understand the error message and its reason. Please, please do not tell me why this should be avoided!!!
To make the error message and (more important) the potential of having an error go away, I have introduced a scope resolution operator. So the constructor now looks like that:
MyClass::MyClass()
{
MyClass::compute();
}
I would expect that to be absolutely safe. But Eclipse code analysis continues to display the error message. Why? It is not possible to call an unexpected method. And if the method was pure virtual in MyClass
, the compiler would tell me so.
Is this an Eclipse code analysis bug or am I missing something?
Edit: the scope resolution operator is supposed to suppress virtual resolution. Therefore I expect it to prevent all possible errors mentioned by the warning. Is my assumption true?
I would like to summarize the most important points from the comments/chat: