2 Replies - 2483 Views - Last Post: 21 August 2010 - 01:54 PM Rate Topic: -----

#1 harris2107  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-August 10

how can we call constructor and destructor explicitily in C++?

Posted 21 August 2010 - 01:22 PM

how can we call constructor and destructor explicitily in C++?
Is This A Good Question/Topic? 0
  • +

Replies To: how can we call constructor and destructor explicitily in C++?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: how can we call constructor and destructor explicitily in C++?

Posted 21 August 2010 - 01:52 PM

Yes, but not on already instantiated (instances) of an object. For instance if you wanted one constructor to call another you can do that. But you could not call the constructor as if it was another method on an object that is already instantiated.

Hope you get it now. :)
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: how can we call constructor and destructor explicitily in C++?

Posted 21 August 2010 - 01:54 PM

you should never have to call the destructor explicitly, rather you could create a function to simulate the destructor
class Foo
{
    int *obj;
    
 public:
    Foo(void)
       :obj(new int);
    ~Foo(void)
     {
         clear();
     }
 
    void clear(void)
    {
          if(obj!=0)
              delete obj;
    }
};
   


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1