Jump to content
  • Advertisement
Sign in to follow this  
Mco

What is the colon for after the parameters in a function definition in c++?

This topic is 3789 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, Im fairly new to c++ and Im slightly confused about the following function definition: IvVector3::IvVector3(const IvVector3& other) : x( other.x ), y( other.y ), z( other.z ) { } It's the colon part after the parameters and before the function body that is confusing me. Is this the same as: IvVector3::IvVector3(const IvVector3& other) { x = other.x; y = other.y; z = other.z } If so what is the difference doing it the first way? Thanks for your time :)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Mco
If so what is the difference doing it the first way?


The first version initializes the members with the provided value. The second version initializes the members with default values, and then assigns the provided value to them.

Share this post


Link to post
Share on other sites
It's called an 'initializer list'. This thread touches on the topic a bit, and also includes a link to a C++ FAQ entry that should help clear things up for you.

Share this post


Link to post
Share on other sites
There's a couple of reasons to use initializer lists instead of assigning in the constructor's body:

1) Clarity, it's clearer and more concise that you're perform initialisation and you don't have to worry about prefixing everything with 'this.' if your parameter names are the same as your member variable names (eg: IvVector3::IvVector3(float x, float y, float z) : x(x), y(y), z(z) { })

2) Performance, assigning in the constructor's body means that the member variables are first default constructed, then assigned (using IvVector3::IvVector3() and IvVector3::operator=(const &IvVector3& v) respectively). With builtin types this won't make any difference, but for custom types with non-trivial/expensive constructors this double initialisation can hurt you performance-wise.

There's also reason the not use the initialiser list:

1) You cannot initialise arrays in the initialiser list, the syntax just doesn't exist in C++.

2) If you want to pass 'this' to a member variable's constructor. While allowed you can run into problems if the member's constructor tries to use the 'this' pointer immediately as your object may not have finished initialising yet.

3) If you want to pass a member variable into another member variables's constructor. Member variable's aren't constructed in the order they appear in the initialiser list, but rather in the order they're declared in the class. So again, while this is allowed it's usually a good idea to avoid it.

For example:

class MyClass
{
C c;
B b;
A a;
public:
MyClass() : a(), b(this), c(b) { }
void Func() { a.Func(); }
};

This might seem OK by looking at the constructor, but the order of initialisation is really c(b), b(this), a(). C's constructor will be getting passed an uninitialised 'b', and if B's constructor tries to call this->Func() it'll end up calling A::Func() with an uninitialised 'a'.

Share this post


Link to post
Share on other sites
Thanks for the quick replies :)

That's really cleared things up for me, especially the comprehensive reply from joanusdmentia.

That's also a very handy C++ FAQ!!!

Thanks!!

Share this post


Link to post
Share on other sites
Also, to be pedantic, that is not a function, rather it is a "constructor" which is a special method used to initialize an instance of a class or struct.

You cannot apply the member list initializer syntax to other methods or functions, only to constructors.

Share this post


Link to post
Share on other sites
Quote:
Original post by ravyne2001
Also, to be pedantic, that is not a function, rather it is a "constructor" which is a special method used to initialize an instance of a class or struct.


To be pedantic, C++ doesn't have "methods", it has member functions. Constructors are a type of member function, and are still functions.

Share this post


Link to post
Share on other sites
/me tears hair out.

This is easily one of the most commonly asked questions about C++. Why does seemingly noone know about such a basic language feature, ten years later?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Why does seemingly noone know about such a basic language feature, ten years later?
Because they're "fairly new to c++ and slightly confused"?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
/me tears hair out.

This is easily one of the most commonly asked questions about C++. Why does seemingly noone know about such a basic language feature, ten years later?


Because the recommended way to learn C++ is to learn C first. Or Java. Or better yet, any other language which has single-form constructors.

And to bring us back a point raised in another thread:
Why doesn't anybody teach RAII? Constructors are essentially implied there.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!