Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
C++: Destructor initialization
Results 1 to 4 of 4

Thread: C++: Destructor initialization

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Posts
    417

    C++: Destructor initialization

    Hi,


    Suppose I had a class w/ a custom destructor.

    if I created an instantiation of this class, did what I wanted w/ it, then wanted to delete it right away....would I call on it's destructor like this?

    myobject.~ClassName();

    the data member is a vector of pointers to linklists. the destructor empties the linklists then the vector.

    thanx,

  2. #2
    Associate
    Join Date
    Dec 2002
    Posts
    13

    Re:C++: Destructor initialization


    I guess you already know this, but normally the destructor will be called automatically when the time has come for your object.

    If you created the object itself dynamically (with "new&quot, then you need to destroy the object yourself with "free". In this case also the destructor will be called automatically.

    In other cases, yes, you would call the destructor like you described. In this case, (if you destructor is correct) the object frees all the memory it allocated dynamically, but the object itself will continue to exist until its time cometh. AFAIK it is not very common to do this.

  3. #3

    Re:C++: Destructor initialization

    [quote author=Hko link=board=9;threadid=6150;start=0#58442 date=1043266998]If you created the object itself dynamically (with "new&quot, then you need to destroy the object yourself with "free". In this case also the destructor will be called automatically.[/quote]

    actually free is for C, in C++ delete should be used, example:

    Code:
     obj *o = new obj;
     o->doSomething(10);
     o->print();
     delete o; /* this calls destructor */

  4. #4

    Re:C++: Destructor initialization

    gorn's right. the delete keyword calls the destructor, provided you used proper syntax in the definition (~Classname). However I feel I should add the obvious and remind you to set your pointer to null immediately following the delete statement. Just a friendly reminder

Similar Threads

  1. eth0 initialization & bootup failure
    By destinationme in forum Linux - Hardware, Networking & Security
    Replies: 1
    Last Post: 01-06-2005, 12:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •