Calling the Constuctor

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Calling the Constuctor

Post by Asimov » June 4th, 2012, 12:33 am

Hi Chilli,

I am kinda relearning C++ after quite a long break, but I want to understand something about constructors.

Since following your tutorials I have been putting the code in the constructor like this.

Code: Select all

Missile::Missile()
	:x(400 ),
	y( 580 ),
	Fired ( false),
	Velocity ( 5 ),
	Explode ( false ),
	radius (0)
{
}
But when I used to program constructors I used to put the code in the curly brackets ie

Code: Select all

Missile::Missile()
{
int Explode= 10;
int radius =5;
}
Is this wrong, or are the functions of the curly brackets different?
My programs ued to run ok, but I would need to call the constructor to iniaialise variables.
Of course I used to keep my functions and classes together before as well.

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

uglypie
Posts: 21
Joined: May 5th, 2012, 5:05 pm

Re: Calling the Constuctor

Post by uglypie » June 4th, 2012, 7:52 am

The first type of syntax is called an initializer list. This is of interest if your class inherits from some base class, and you want something other than the default constructor to be called.

Ex.

Code: Select all

class Shape
{
public:
   Shape();
   Shape(int a);
};

class Rectangle : public Shape
{
public:
  Rectangle ()
  {
    // The Shape default constructor has already been called
    // Do some default stuff
  }
  Rectangle (int a)
  : Shape(a)
  {
    // The non-default constructor for Shape has been called
    // Initialize the other stuff
  }
};
For standard datatypes such as int and float, there's no difference.

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Calling the Constuctor

Post by Asimov » June 4th, 2012, 10:16 am

Hi Ghillie,

I have a lot of books on C++ and I don't think I have seen any where they put the initializer above the curly brackets, but it works so I will carry on doing it heh heh.

Thank you for clearing that up.

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

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

Re: Calling the Constuctor

Post by chili » June 4th, 2012, 3:06 pm

Chili

ghilllie
Posts: 72
Joined: May 2nd, 2012, 3:25 am

Re: Calling the Constuctor

Post by ghilllie » June 4th, 2012, 7:19 pm

Asimov wrote:Hi Ghillie,

I have a lot of books on C++ and I don't think I have seen any where they put the initializer above the curly brackets, but it works so I will carry on doing it heh heh.

Thank you for clearing that up.

Asimov
Hey looks like a bit missed the address :-)
Chili++ for President :)

ghilllie
Posts: 72
Joined: May 2nd, 2012, 3:25 am

Re: Calling the Constuctor

Post by ghilllie » June 4th, 2012, 7:20 pm

Google OPS! bro not working...
Chili++ for President :)

Post Reply