Difference between this->CMyClass::CMyClass() and CMyClass()
Why does it have to be this complicated to call a constructor
The purpose of this writing is to illustrate how constructors can be called in Visual Studio.
I am not suggesting to call constructors explicitly. Here is the queston:
Why does it have to be this complicated to call a constructor:
this->CMyClass::CMyClass();
instead of just CMyClass();
See function f() in the following example.
struct CMyClass
{
int mi;
CMyClass()
{
printf("%d\n", mi);
}
void f()
{
this->CMyClass::CMyClass();
}
};
In this->CMyClass::CMyClass();
'this
' is required; Otherwise it creates a temporary object instead of calling constructor.
'CMyClass::
' is required; otherwise CMyClass
is interpreted as a type instead of a constructor.
I also made a test case on my blog