Thread: Constructors And Destructors... What's The Point?

  1. #1
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37

    Constructors And Destructors... What's The Point?

    See above question.
    -Dean

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    See the answer below.

    Constructors are used to allocate memory needed by the object and initialise data. Destructors are used to free memory, typically allocated by the constructor.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  3. #3
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37
    Hmm...

    Constructors dont even really take up much memory.
    -Dean

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >Constructors dont even really take up much memory

    I don't understand what you mean.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  5. #5
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    >>Constructors dont even really take up much memory.<<

    Constructors aren't what take up the memory. The object is what takes up the memory. The constructor is just the name of the method used to create an instance of the object.

  6. #6
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37
    Hmmm.....

    I'm still not seeing the point of them. Could someone just give me an idiot's guide to constructors?
    -Dean

  7. #7
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    OK. Let's suppose you have an object for dealing with a lot of text - say a document.

    Where is that object going to store the text? The answer is that it will allocate memory to do this.

    When does it need to allocate the memory? Usually when the object is created.

    So where does it allocate the memory? Ideally in a method which is automatically called when the object is created. Such a method is a called a contructor.

    What about freeing the memory once it's no longer needed? Yes this must be done, otherwise you'd have memory leaks.

    So where should the memory be freed. Ideally in a method which is called just before an object is destroyed. Such a method is called a destructor.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    First of all DeanDemon, do you know what's is Dynamically Allocating memory? and how to do it?

    I think you'll be more able to understand constructors, but evern if you don't there are another uses that don't require dynamic momory allocation:
    Code:
    class RaionalNumber {
    public:
    
    
    private:
    int numerator;
    int denominator;
    };
    suppose you have that class and you created an object
    Code:
    RationalNumber r1;
    ;
    it won't be initialized, and you cann't initialize.
    but if you have a constructor:
    Code:
    class RaionalNumber {
    public:
    RationalNumber( int x=1, int y=1 ){
         numerator = x;
         denominator = ( y != 0 ? y : 1 )
    }
    
    private:
    int numerator;
    int denominator;
    };
    so you can creat objects like this:
    [code]
    RationalNumber r2(1, 2); //numerator=1, denominator=2...

    But again constructors are more useful when dealing with pointers and dynamic memory allocaion.
    Did you get the point
    none...

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, 05: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, 02:08 PM
  4. Constructors and Destructors
    By GravtyKlz in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2003, 10:44 AM
  5. Classes: constructors, destructors ???
    By mbeisser21 in forum C++ Programming
    Replies: 18
    Last Post: 07-21-2002, 09:33 PM