destructor & constructor question

This is a discussion on destructor & constructor question within the C++ Programming forums, part of the General Programming Boards category; Hi, i made this program that does nothing to see when the constructor and destructor are called. I have a ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    destructor & constructor question

    Hi, i made this program that does nothing to see when the constructor and destructor are called. I have a question, shouldnt the contructor and destructor have been called 3 times each instead of just two.....can you explain this to me please. Here is my code:-

    Code:
    #include<iostream>
    using namespace std;
    
    class lets  
    {
    public:
    	lets();
    	virtual ~lets();
    	char *t;
    
    };
    
    lets::lets()
    {
    
    cout<<"called constructor"<<endl;
    }
    
    lets::~lets()
    {
    cout<<"called destructor"<<endl;
    
    }
    
    
    int main()
    {
    	lets y;
    	lets k;
    
    	y.t=new char;
    	delete y.t;
    
    	{
    		k.t=new char;
    		delete k.t;
    	}
    
    	return 0;
    
    }

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Virtual destructors?

    Hmmm.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    I made the class file with the help of the new class function of Microsoft Visual C++. Thats how it makes the constructors. i put everything into the main file to post here.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Nope, two lets objects means two constructors and two destructors.

    lets y; //first constructor call
    lets k; //second constructor call

    return 0; //two destructor calls as y and k go out of scope.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    what gave you the idea that they would be called 3 times? I'm a little confused.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    I thought as soon as the program starts the constructor is called, then again 2 more times for the objects created. Then everytime i called delete(2 times) and once when the program ended.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Constructors are called upon instantiation. Their creation has nothing to do with when the program starts.

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Bubba
    Virtual destructors?

    Hmmm.
    If your class is to be derived from and used polymorphically, then you should make your destructors virtual.

    VC++6 does this by defualt

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Interesting. I've never created a virtual destructor - at least not to my knowledge.

    I thought that if you provided a destructor in derived classes, those destructors would be called - as well as the base class destructor. Therefore what would be the point of declaring a destructor as virtual since you can redefine destructors relative to their specific classes anyways?

    In theory are not all destructors virtual since they can be redefined in each class?

  10. #10
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    In theory are not all destructors virtual since they can be redefined in each class?
    But they can't.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Bubba, if class B is derived from class A and A's destructor is not virtual the destructor for B will not call the destructor for A.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Bubba
    I thought that if you provided a destructor in derived classes, those destructors would be called - as well as the base class destructor. Therefore what would be the point of declaring a destructor as virtual since you can redefine destructors relative to their specific classes anyways?
    If you create the object on the heap using a base object, and then call delete on the pointer, only the base destructor will be called

    Code:
    #include <iostream>
    
    class base
    {
    public:
    	base(){std::cout << "base Constructor" << std::endl;}
    	~base(){std::cout << "base Destructor" << std::endl;}
    };
    
    class derived : public base
    {
    public:
    	derived(){std::cout << "derived Constructor" << std::endl;}
    	~derived(){std::cout << "derived Destructor" << std::endl;}
    };
    
    
    int main(void)
    {	
    	base *ptrBase = new derived;
    
    	delete ptrBase;
    }
    Now make ~base() virtual and note the difference..

    [edit]FIB got there before me[/edit]

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I didn't explain it as well as you did Fordy. actually I think I explained it really poorly. What I meant to say was that deleting an A pointer to a B would not call B's destructor.

    edit:
    of course that's not easy to follow either. just look at what fordy wrote
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #14
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Bubba, if class B is derived from class A and A's destructor is not virtual the destructor for B will not call the destructor for A.
    But if the destructor for class A is written correctly, then all elements of class A will be deleted correctly. Likewise since class A's destructor is not called because B is derived from A - when B is destroyed, B's destructor will be called.

    So the point is that if you make class A's destructor virtual, the compiler will call down the chain through all of the destructors until it arrives at the base destructor? This would ensure that all objects were destroyed correctly.

    Interesting, I've never used this and have never really given it any thought. Perhaps I might use it in the future.

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Bubba
    But if the destructor for class A is written correctly, then all elements of class A will be deleted correctly. Likewise since class A's destructor is not called because B is derived from A - when B is destroyed, B's destructor will be called.

    So the point is that if you make class A's destructor virtual, the compiler will call down the chain through all of the destructors until it arrives at the base destructor? This would ensure that all objects were destroyed correctly.

    Interesting, I've never used this and have never really given it any thought. Perhaps I might use it in the future.
    What it means is that "delete" knows nothing of the object it is freeing except the type of the pointer passed to it. It doesnt know that there is more to the object....as far as it's concerened, it has a "base" object so it calls the "base destructor". Now if that destructor is virtual, any object derived from "base" will have it's destructor called as well as the "base destructor"

    Bubba (and anyone else that hasnt read them), I seriously recommend you check Effective C++ and More Effective C++ from Scott Meyers. These books point out important details like this and I guarantee you will benefit for having read them - they are also a very good read IMO....many C++ texts are a little dry (understandably)...but these books are really enjoyable to read

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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