65.9K
CodeProject is changing. Read more.
Home

Difference between this->CMyClass::CMyClass() and CMyClass()

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (3 votes)

Nov 24, 2011

CPOL
viewsIcon

18840

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