Constructors and Destructors

This is a discussion on Constructors and Destructors within the C++ Programming forums, part of the General Programming Boards category; Are constructors called as soon as the program starts and then never used again? When are Destructors called? Thanks...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    31

    Constructors and Destructors

    Are constructors called as soon as the program starts and then never used again? When are Destructors called? Thanks

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    A constructor is called when its class is initialized as an object. A deconstructor is called when the object is put out of play.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    A deconstructor is called when the object is put out of play.
    Is that typically at the end of the program? Im a C programer learning C++ so Im not fluent with objects just yet.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    a constructor is called when an object needs to be constructed. This could be when a temporary object is made. For instance a call by value always calls a copy constructor to copy the value to the stack.
    a destructor is called immediately an object goes out of scope. This could be tiggered by somthing as simple as closing a block.
    Learn about scope of variables. When a variable is made a constructor is called and when it is no longer in scope its destructor is called.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    That makes more sense....Im thinking in terms of global class so I guess it would be called at the start of the program and deconstructed at the end. I definitely prefer classes over structs now.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Yeah, just to further illustrate what these guys said, here's some code that might help you.

    Code:
    void foo() {
    	MyClass test_class;
    	//MyClass's constructor just got called because an object instance
    	//of it was declared
    
    	//MyClass's destructor will be called when the end of the function
    	//comes because the object will go out of scope
    }
    I don't know if you are familiar with the new and delete operators, but here's an example using them:


    Code:
    void foo() {
    	MyClass* test_class;
    	//Constructor is NOT called here because you haven't created an
    	//object of MyClass, just a pointer to an object
    
    	test_class = new MyClass;
    	//Here, constructor is called because you are creating an object
    	//of MyClass with the new operator
    
    	delete test_class;
    	//Here the destructor is called because you are destroying the
    	//object
    }
    Hope that helps a little bit
    Last edited by PorkyChop; 03-09-2003 at 10:43 AM.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    That makes more sense....Im thinking in terms of global class so I guess it would be called at the start of the program and deconstructed at the end. I definitely prefer classes over structs now.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    I dont know where that last post of mine came from...but in any case I understand now. Thanks a lot for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a class have multiple destructors?
    By meili100 in forum C++ Programming
    Replies: 1
    Last Post: 05-14-2008, 06:28 PM
  2. Constructors and Destructors
    By lasher in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2006, 11:53 PM
  3. Destructors and Constructors on classes
    By Da-Nuka in forum C++ Programming
    Replies: 14
    Last Post: 06-14-2005, 03:08 PM
  4. Constructors And Destructors... What's The Point?
    By DeanDemon in forum C++ Programming
    Replies: 7
    Last Post: 12-15-2002, 12:47 PM
  5. Classes: constructors, destructors ???
    By mbeisser21 in forum C++ Programming
    Replies: 18
    Last Post: 07-21-2002, 10:33 PM

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