Thread: Constructors

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    Constructors

    What is the proper decent way to use constructors (default as well). what purpose do they serve?

    I can use constructors on classes, but I am only retyping code not really sure what it means

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use constructors to initialize any member data in your classes, as well as "ready" the class for use. Allocate any memory, acquire any resources, etc, etc.
    Destructors should do the opposite. They should clean up everything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Ok so I use a constructor to ensure that there isnt junk values stored in a variable. rather use the constructor to initialize a variable to zero than a random value like 43535

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or whatever you want them to be before starting to use the instance of the class.
    The variables have a meaning and it's the constructor's job to make sure they get their meaning and not have some junk stored inside them.
    But this is not all a constructor can do.

    Say there's some API you want want to call inside the class.
    Your class's constructor can call the initialize function for the API so the rest of the class can call and use that API later.
    The destructor will call terminate, if there's such a function in the API.

    It depends on your need - but the constructor's job is to initialize the class state to make it ready before using it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:20 PM
  2. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  4. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 07:35 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM