Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   calling ctor inside a member function of a class (http://www.go4expert.com/forums/calling-ctor-inside-function-class-t2048/)

Frank Reich 29Nov2006 09:39

calling ctor inside a member function of a class
 
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 29Nov2006 10:28

Re: calling ctor inside a member function of a class
 
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.


All times are GMT +5.5. The time now is 00:05.