Deleting classes, or is it impossible?

This is a discussion on Deleting classes, or is it impossible? within the C++ Programming forums, part of the General Programming Boards category; A long time ago, in a galaxy far far away i saw a c++ book. It somehow learnt me much, ...

  1. #1
    Lariumentiko
    Guest

    Deleting classes, or is it impossible?

    A long time ago, in a galaxy far far away i saw a c++ book.
    It somehow learnt me much, and now i want to, not really delete classes, but say i declare a class called "cat". yep, that was what the book said. and then make the destructor like

    "cat::~cat(){
    cout << "The cat is dead";
    }"

    well, then. if i declare a "cat" then how do i delete it?
    Tell me if i have to be more specific.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Unless you use the new and delete operators, the destructor will be called when the variable goes out of scope.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Lariumentiko
    Guest
    This should be the code:

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    class cat{
    private:
    	int age;
    public:
    	cat();
    	~cat();
    	setage(int newage){age=newage;};
    	getage(){return age;};
    };
    
    cat::cat(){
    cout << "a cat is born" << endl;
    }
    
    cat::~cat(){
    	cout << "a cat died" << endl;
    }
    
    int main(){
    	int input_number;
         cat frisky;
    	cout << "Just a line..." << endl;
    	cin >> input_number;
    	if (input_number==1){
    		delete frisky; // how do i do this?
    	}
    	cout << "this should be the last line" << endl;
    	return 0;
    }
    any help?

  4. #4
    Lariumentiko
    Guest
    Originally posted by XSquared
    Unless you use the new and delete operators, the destructor will be called when the variable goes out of scope.

    hmm... what did you just say? how do i do that... i think i heard of it once...

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    It depends on how the cat was born. If it was declared normally inside {}'s it dies when it hits the enclosing } (called leaving scope) if it was declared globally (outside of any {}, global scope) it dies when your program exits. If it was declared with new, the cat is imortal,only then do you have to go hunt it down and kill it yourself with delete.

    Code:
    cat globalcat;
    int main() {
      cat a;
      cat *b = new cat();
      cat *leaky_cat = new cat();
      {
           cat c;
      }// cat c dies here
    ...
      delete b; // cat b dies here.
      return 0; // cat a dies here
    }
    // global cat dies sometime after the return 0;
    // leaky_cat never "dies", though the 
    // memory used may be returned to 
    // other programms sometime after this 
    // programm ends

  6. #6
    Lariumentiko
    Guest
    Nice, grib. but...
    If the cat is a pointer(isn't it?), then how to use the functions and set member variables of the cat? like
    Code:
    cat tom;
    tom.setage(2);// this works the same way?
    //and pretend there is a PUBLIC int
    //member of cat class called luckynumber
    
    cout << tom.luckynumber;// then how does this work?
    please help me

  7. #7
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    If you want to specificially destroy your cat without using delete at a specific place, you can create it within an extra {} block, ie:

    Code:
    int main (void)
    {
        cat Kitty;
    
        {
             cat Lucy;
             Lucy.Meow();
        } // Lucy's deconstructor is called
    
        return (0); // Kitty's deconstructor is called
    }
    and um, I don't really understand your last question, but to access member variables/functions of a pointer to class use ->, to access normally created used the dot operator .
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  8. #8
    Lariumentiko
    Guest

    Re: ~

    Originally posted by rmullen3
    If you want to specificially destroy your cat without using delete at a specific place, you can create it within an extra {} block, ie:

    Code:
    int main (void)
    {
        cat Kitty;
    
        {
             cat Lucy;
             Lucy.Meow();
        } // Lucy's deconstructor is called
    
        return (0); // Kitty's deconstructor is called
    }
    and um, I don't really understand your last question, but to access member variables/functions of a pointer to class use ->, to access normally created used the dot operator .

    Ok. So i can just add some "{" and "}" without making any extra function? If not, then what i meant before was: a pointer is a variable that points on another variable, just like a sign. I'ts not like another cat pointing at the other cat? I heard of that some time before but it really works?(or well, pointing at a place in memory with the value)

    please tell me

  9. #9
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Code:
    #include <iostream>
    
    using namespace std;
    
    class cat{
      public:
            cat(){
                    cout << "cat born\n";
            }
    
            ~cat(){
                    cout << "cat died\n";
            }
    
            void meow(){
                    cout << "Meow!\n";
            }
    };
    
    int main(){
    
            cat *c = new cat();
    
            c->meow();
    
            delete c;
    
    }
    prints

    cat born
    Meow!
    cat died

  10. #10
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>If you want to specificially destroy your cat without using delete at a specific place

    Thats a little hackish. And if Lucy had been allocated with new then obviously that wouldnt work. You can call a class destructor directly. The variable would still be available afterwards, but thats no loss.

    cat * Kitty = new cat;

    cat->~cat(); //Died

    We're not taking care of the memory pointed to by Kitty, but we're taking care of anything inside Kitty dealt with by the destructor. (In this case, displaying text.)

    So a better method would be:
    Code:
    int main (void)
    {
        cat Kitty;
    
        cat Lucy;
        Lucy.Meow();
        Lucy.~cat(); // Lucy's deconstructor is called
    
        //we could still use Lucy here but we probably shouldn't, since technically she died,
        //and playing with dead things is never a good idea.
    
        return (0); // Kitty's deconstructor is called
    }
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    You can call a class destructor directly
    Bang, you're dead.
    Joe

  12. #12
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    A comment:
    When you are calling the destructor, you are not destroying the class you are destroying the object of that class.
    none...

  13. #13
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    >>Bang, you're dead.

    Yes, true. I should have covered that one a little better.

    >>When you are calling the destructor, you are not destroying the class you are destroying the object of that class.

    Very important to note.

    Personally, I dont believe I've ever called a destructor manually. I merely pointed out that should the need arise, it would be possible to do so. If there is a need (though I know not what) to do such a thing I would definatly be careful. There are plenty of things that could go wrong, yet also plenty of safe ways to do it.

    My solution has always been this one. I highly recomend it over any other option. Failing that, your destructor should be made failsafe in such a way that it will not perform actions that will result in impossible conditions when it is called by default, or by not allowing those conditions to occur on the default call.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  14. #14
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    lightatdown is right, you never call a destructor, that's done automatically, when the variable gets out of scope.
    But you can call it, I mean the compiler won't complain, even though it's not correct!
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting derived classes
    By *DEAD* in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2008, 12:37 PM
  2. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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