Explicitly calling a constructor/destructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    Explicitly calling a constructor/destructor

    After i create an object can i call the constructor again to reinitialize that objects variables? I know i can create a function to do this, but i was wonder if i can just call the constructor again. If i can call the constructor again is this a good practice or should i avoid such a use of the constructor. Here is some test code.
    [CODE=cpp]
    //code created and tested in VS 2003
    #include <iostream>

    using namespace std;

    class TestClass
    {
    public:
    TestClass();
    ~TestClass();
    void ModifyVariables (int, int);
    void PrintVariables( );
    private:
    int Variable1;
    int Variable2;
    };

    TestClass::Test Class()
    {
    Variable1 = 100;
    Variable2 = 100;
    }

    TestClass::~Tes tClass()
    {
    Variable1 = 0;
    Variable2 = 0;
    }

    void TestClass::Modi fyVariables(int Mod1, int Mod2)
    {
    Variable1 += Mod1;
    Variable2 += Mod2;
    }

    void TestClass::Prin tVariables()
    {
    cout<<Variable1 <<" "<<Variable2<<e ndl;
    }

    int main()
    {
    TestClass TestObject;
    TestObject.Prin tVariables();
    TestObject.Modi fyVariables(20, 45);
    TestObject.Prin tVariables();
    //TestObject.Test Class(); //can't do
    TestObject.~Tes tClass(); //works just fine.
    TestObject.Prin tVariables();
    TestObject.Modi fyVariables(120 , 145);
    TestObject.Prin tVariables();

    return 0;

    /* OUPUT:
    100 100
    120 145
    0 0
    120 145
    */
    }
    [/CODE]
    why am i able to call the destructor like that and not the constructor?
    How can i call the constructor to reinit the variables to 100? or do i have to write a separate function?
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by Studlyami
    After i create an object can i call the constructor again to reinitialize that objects variables? I know i can create a function to do this, but i was wonder if i can just call the constructor again. If i can call the constructor again is this a good practice or should i avoid such a use of the constructor. Here is some test code.
    [CODE=cpp]
    //code created and tested in VS 2003
    #include <iostream>

    using namespace std;

    class TestClass
    {
    public:
    TestClass();
    ~TestClass();
    void ModifyVariables (int, int);
    void PrintVariables( );
    private:
    int Variable1;
    int Variable2;
    };

    TestClass::Test Class()
    {
    Variable1 = 100;
    Variable2 = 100;
    }

    TestClass::~Tes tClass()
    {
    Variable1 = 0;
    Variable2 = 0;
    }

    void TestClass::Modi fyVariables(int Mod1, int Mod2)
    {
    Variable1 += Mod1;
    Variable2 += Mod2;
    }

    void TestClass::Prin tVariables()
    {
    cout<<Variable1 <<" "<<Variable2<<e ndl;
    }

    int main()
    {
    TestClass TestObject;
    TestObject.Prin tVariables();
    TestObject.Modi fyVariables(20, 45);
    TestObject.Prin tVariables();
    //TestObject.Test Class(); //can't do
    TestObject.~Tes tClass(); //works just fine.
    TestObject.Prin tVariables();
    TestObject.Modi fyVariables(120 , 145);
    TestObject.Prin tVariables();

    return 0;

    /* OUPUT:
    100 100
    120 145
    0 0
    120 145
    */
    }
    [/CODE]
    why am i able to call the destructor like that and not the constructor?
    How can i call the constructor to reinit the variables to 100? or do i have to write a separate function?
    Constructors is called by the compiler internally and i dont think so we can call the constructor explicitly.
    You can call the destructor explicitly, as there is case for this.
    That is if we use placement new then we have to call the destructor explicitly.
    I think becos of this explicit calling of destructor is being allowed.
    Other members please correct me if i am wrong.

    Raghuram

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      When the compiler creates an object, it calls your constrcutor to initialize the variables.

      When you call the constructor, the compiler first creates an object and then uses the constructor call tio initialize it.
      [code=c]
      void func(string& arg)
      {

      }
      int main()
      {
      func(string("He llo"));
      }
      [/code]

      Here the string constructor call results in the creation of a temporary string object containing "Hello" that can be used as the reference argument of the function call.

      That means you cannot re-initialize an existing object using a constructor.

      Comment

      Working...