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


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();
	}
};

void GeneralTest()
{
	CMyClass a;
	a.mi = 2;
	a.f();
}

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.

The difference between with ‘this’ and without ‘this’ can be tested by GeneralTest(); just watch the output.
(This code runs as expected on Visual Studio 2010, which has some C++11 features built in)

Advertisements

2 Comments

  1. I am getting error while compiling this code:

    http://codepad.org/7N5Qbfxu

    • Rio

       /  December 3, 2011

      @mitalvora
      It was on Visual Studio 2010, which has some C++11 features built in. I am adding a note to the original post. Thanks.

%d bloggers like this: