Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

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

1.00/5 (3 votes)
30 Nov 2011CPOL 18.8K  
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:
C++
this->CMyClass::CMyClass();

instead of just CMyClass();

See function f() in the following example.
C++
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)