calling ctor inside a member function of a class

Frank Reich's Avatar
Go4Expert Member
Hello. May I ask a question to the ctor in this program.

main.cpp:
Code:
#include <iostream>
#include "cTest.h"

int main()
{ 
	cTest i;

	i.SetA(5);

	i.ClearA();

	return 0;
}
cTest.h:
Code:
#pragma once

class cTest
{
public:
	cTest(void)
	{itsA = 0;}
	~cTest(void){}
	void SetA (int i)
	{itsA = i;}
	void ClearA ()
	{cTest();}
private:
	int itsA;
};
I am wondering, why the value of 'itsA' is not '0' after 'i.ClearA();' was called in main.cpp.

Thanks.
F.R.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Its because when you are calling a constructor its creating a new object and assigning the value 0 and as the object goes out of scope its destroyed and so the object in the main does not reflect zero.