constructors?

This is a discussion on constructors? within the C++ Programming forums, part of the General Programming Boards category; Im having trouble, what exactly is a constructor?...

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

    constructors?

    Im having trouble, what exactly is a constructor?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    a function that get's called on creation of an object. It's what you might have done with InitStruct(&thestruct); in C but it get's called automatically
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    A constructor is nothing compared to a destructor.

    But seriously...a constructor just initializes all your member fields to an initial value. Trust me, you'll come across this situation sooner or later, then you'll say "Ah. That's what its for."

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    It's a guy in a hardhat that eats from a lunch truck..

    A constructor is a public (normally) function that belongs to a class. When a new object is created, the appropriate constructor (there can be more than one) is automatically called. They are usually used to initialize member variables, call parent class constructors, and really just class initialization in general.

    (I know you didn't ask, but) A deconstructor is similar to a constructor. There may only be one deconstructor in a class. It too is called automatically, but when an object goes out of scope and is used to deinitialize an object (ie: deleting allocated memory, freeing resources, etc).

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, 08:20 PM
  2. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 07:21 PM
  4. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 08:35 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM

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