I came upon some old code which uses a rather strange method for calling a static function of a class:
Class definition and implementation
class MyClass
{
public:
...
static MyClass *GetFromString(const char *string);
...
};
MyClass *MyClass::GetFromString(const char *string)
{
...
}
Calling of GetFromString in the old code
MyClass *pClass = pClass->GetFromString("TEST");
IMO the proper way would be this
pClass = MyClass::GetFromString("TEST");
In the first method it looks as if we dereference an uninitialized this pointer, but as the GetFromString
method is static, the this pointer is actually not needed and the code works.
What does the standard say ? Is the first method correct or could it lead to undefined behaviour with some compilers ?
I'm not sure if this is a duplicate of this SO question because I explicitly mean static member functions.