Question about using "delete" and pointers

This is a discussion on Question about using "delete" and pointers within the C++ Programming forums, part of the General Programming Boards category; Is there any way to delete a class from its pointer, or do you just have to have a function ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    Question Question about using "delete" and pointers

    Is there any way to delete a class from its pointer, or do you just have to have a function in the class that you can call something like:

    Code:
    void CClass::DeleteClass()
    {
         delete this;
    }
    Last edited by kurtmax_0; 09-16-2003 at 09:03 PM.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The destructor will be called when you call delete on a pointer to a class. For example,
    Code:
    class A {};
    
    ...
    A* obj;
    delete obj; // Calls ~A()
    You should avoid kamikaze classes which has 'delete this' somewhere (anywhere) within them. It is easy to free the memory internally from the class while still assuming it is valid outside (and therefore trying to access invalid memory).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    5
    Thanks, that helps. Now I can not put "kamikaze" classes in my code. I was having problems accessing invalid memory....

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    In fact, the ONLY thing you should ever call delete on is a pointer to a dynamically allocated object. In general, you need a 1:1 pairing of new and delete, a well as new[] and delete[].
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Agreed. The only "good" use of 'delete this' I have seen was for compatibility with an old (large and deeply embedded) library in an otherwise well designed OOP program.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about pointers in C
    By Kilua in forum C Programming
    Replies: 3
    Last Post: 06-05-2009, 02:33 AM
  2. Pointers Question
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 09-08-2008, 10:17 AM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question about pointers
    By maxhavoc in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2004, 12:46 AM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 10:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21