constructor and destructor

This is a discussion on constructor and destructor within the C++ Programming forums, part of the General Programming Boards category; why would I use them what do they do I dont understand them...

  1. #1
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    constructor and destructor

    why would I use them what do they do I dont understand them
    +++
    ++
    + Sekti
    ++
    +++

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    To intialise your object, and clean up after it. What don't you understand?

  3. #3
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    well

    why I would use them
    +++
    ++
    + Sekti
    ++
    +++

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Constructor -

    Say you had a system that relied on everything being initialised to the correct value. Say you had a bunch of amateurs or careless coders working on this system, or that it was just too important that these values get initialised that you couldn't leave it to chance (or if you're working alone and you're prone to forgetfulness). Then you'd create a constructor that relieves the burden of having to manually call a function every time you create/assign/copy an object.

    Destructor -

    Say your object allocates memory. Say you had a bunch of amateurs or careless coders that don't clean up the memory properly when they've finished, or that it is just too important that this memory gets freed (or if you're working alone and you're prone to forgetfulness). Then you'd create a destructor for the object that relieves you of the burden of having to call a function to free memory.

    Initialising variables and freeing memory isn't hard, but it can be tedious. Constructors and destructors helps free you from the tedium.

  5. #5
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    ok so

    so basicly I do want to learn how to use them
    +++
    ++
    + Sekti
    ++
    +++

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    There are quite a few entry level books/tutorials on C++. Do a search on google.

  7. #7
    Señor Member
    Join Date
    Jan 2002
    Posts
    561
    And I think there's a good tutorial on this site.

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