Question about Constructors

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Paradox
Posts: 22
Joined: July 14th, 2012, 4:11 pm

Question about Constructors

Post by Paradox » November 26th, 2012, 4:09 am

Hi guys, I have recently come across some syntax that looks pretty strange to me.
For example:

Code: Select all

class foo
{
     foo()
};

foo::foo :
     poo( stuff )
{
}
What I'm talking about is the colon in the definition. I've read that its an "initialization list", but that's the purpose of the constructor anyway, isn't it? Please explain? Sometimes I wish I wasn't so curious. 8-)

-Paradox

Muttley
Posts: 39
Joined: October 19th, 2012, 6:00 am

Re: Question about Constructors

Post by Muttley » November 26th, 2012, 4:45 am

Far as I understand, the constructor is called just once when the program starts, so yes, the purpose is to initialize your variables or functions.

Code: Select all

    foo::foo
    : // initialize some simple variables ( I think it is the only thing you can do here,
      // someone correct me if Im wrong  )
      pooX( x ),
      pooY( y ),
      pooZ( z )
    {
         // here you are free, you can initialize things in the way you want,
         // using loops, function calls etc.

         // initialize an array of numbers
         for( int index = 0; index < 10; index++ )
         {
               numbers[index] = index;
         }

          // load an image
          loadImage( "picture.bmp" );

         // initialize a simple variable
         gameover = false;
    }

Paradox
Posts: 22
Joined: July 14th, 2012, 4:11 pm

Re: Question about Constructors

Post by Paradox » November 27th, 2012, 1:45 am

Thanks Muttley, but i'm still confused :(
Far as I understand, the constructor is called just once when the program starts, so yes, the purpose is to initialize your variables or functions.
Right, when a class-object is created it's class constructor is called. The stuff behind the colon is not of an integral type, so what is it? What I've always done is stuff like this:

Code: Select all

foo::foo
{
bar = 10; 
// Do stuff with bar...
}
Not

Code: Select all

foo::foo :
     bar( stuff )
{
// Do stuff.
}
The first way I alter members in the class, without defining anything. The other way... I have no idea. the only thing really confusing is this: foo::foo : bar( stuff )

Again, Thanks for the help
- Paradox
Last edited by Paradox on November 27th, 2012, 3:26 am, edited 1 time in total.

Muttley
Posts: 39
Joined: October 19th, 2012, 6:00 am

Re: Question about Constructors

Post by Muttley » November 27th, 2012, 2:58 am

Both do the same thing,

#1

Code: Select all

foo::foo
: bar( 10 ),
  x( 150 ),
  y( 200 )
 {}
I see the first example as a shortcut to initialize simple variables.

#2

Code: Select all

foo::foo
{
   bar = 10;
   x = 150;
   y = 200;

   for( int index = 0; index < 5; index++ )
   {
       numbers[index] = index;
   }
}
The second example do exactly the same thing, but you can initialize an array with loop if you want, call a function or whatever you want.

Resume:
The first way is a shortcut to initialize simple variables like "bar = 10", you cant initialize the array "numbers" here.
The second way you can initialize anything in the way you want like numbers with a loop.
Both ways are valid.

( my english is kind of sucks, I hope you understand :lol: )

Paradox
Posts: 22
Joined: July 14th, 2012, 4:11 pm

Re: Question about Constructors

Post by Paradox » November 27th, 2012, 3:25 am

Got it! Thanks Muttley! :D

- Paradox

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Question about Constructors

Post by LuX » November 27th, 2012, 9:15 am

In the examples you gave there is no other difference than that the second one basically writes the values twice. If you have "foo::foo ( ) { value = 10; }" what happens is, "value" will first be written as an undefined value and then written again as "10". No biggie, unless you call the constructor a lot in which case it's better to use the first option "foo::foo ( ) : value ( 10 ) { }"

The second thing and maybe the most important is that you can ( or should ) only initialize const values in the pre initialization list. There's probably more, but try to define all the variables you can at the initialization list.
ʕ •ᴥ•ʔ

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Question about Constructors

Post by chili » November 27th, 2012, 1:22 pm

LuX has it mostly. You should initialize as much as possible using the list (as far as "best practices" is concerned).

There are also some cases when you must use the init list. One case is const member vars, as lux pointed out. Another case is references. Lastly, if your class inherits from another class, you call the constructor of the parent class using the initializer list.
Chili

Paradox
Posts: 22
Joined: July 14th, 2012, 4:11 pm

Re: Question about Constructors

Post by Paradox » November 28th, 2012, 2:05 am

Awesome, thanks guys for all your help! BTW, I love the tutorials Chili, keep 'em coming!

- Paradox

Post Reply