Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

Whats this strange code...c++

This topic is 4667 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

Hi, I'm learning about threads, and I've taken a look at ,this person's code. And they have this in it:
class Thread {
private:
    int running;
    unsigned long m_pid;
protected:
    Thread();
};

///this is the strange part.
Thread::Thread() 
    : running(0) 
    , m_pid(0L)
{
}


I've never seen that kind of syntax in c++ before, I guess it's just initializing the two variables, but why wouldn't you just do:
Thread::Thread() 
{
   running = 0;
   m_pid = 0L;
}


which is kind of cleaner. Thanks

Share this post


Link to post
Share on other sites
Advertisement
It's called an initialization list, and you should prefer to use that syntax rather than the one you posted, which assigns values to members after they have already been (default) initialized - usually a waste.

EDIT: some members can only be initialized in the initialization list, const and reference members to name two. linkified term

Share this post


Link to post
Share on other sites
Exactly what stylin said. A little more detail:

When you instantiate an object, each of its members get constructed as well, meaning their constructors get called.

Initializing the members in the main object's constructor (in the second code listing) means the member's constructor gets called and then assignment operator is called.

The second code sample is the effectively the same as saying:
int a;
a = 0;

Most of the time the performance gain does not matter for large complex data structures, but for smaller objects that you'll have a LOT of temps flying around it'll really boost performance. Case in point : a custom 3D Vector class that's used all over a complex physics engine.

Personally I prefer to explicitly initialize the members in my constructor for complex data structures, even though this is not the 'preferred' method. It is, imo, a bit cleaner and easier to debug in certain cases. I only use the initialization list if I really need to squeeze performance out of something (like the custom vector I mentioned above).

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin
Also, initializer lists are the only way to call a base class's constructor from a derived constructor.


I don't understand, can't you do:

DerivedConstructor::DerivedConstructor() {
BaseConstructor::BaseConstructor(); //call it from here.
}

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
I don't understand, can't you do:
*** Source Snippet Removed ***

That will call the base ctor twice.

[edit:] I hadn't even noticed that you calling it as a static method. To correct myself: Calling the base constructor in a derived class outside the initializer list will result in the base ctor being called twice.

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
Quote:
Original post by Sharlin
Also, initializer lists are the only way to call a base class's constructor from a derived constructor.


I don't understand, can't you do:
*** Source Snippet Removed ***



Nope, won't compile.

Share this post


Link to post
Share on other sites
Quote:
Original post by Shannon Barber
Quote:
Original post by johnnyBravo
Quote:
Original post by Sharlin
Also, initializer lists are the only way to call a base class's constructor from a derived constructor.


I don't understand, can't you do:
*** Source Snippet Removed ***



Nope, won't compile.


I just compiled this in visual c++ 2005 express edition beta 2:

class Base {
public:
Base(){}
};
class Derived:public Base {
public:
Derived(){Base::Base();}
};



Seems to work ok, is this what you meant?

Quote:
Original post by silencer-
Quote:
Original post by johnnyBravo
I don't understand, can't you do:
*** Source Snippet Removed ***

That will call the base ctor twice.


Yes I can see that.

Quote:
Original post by Sharlin
Also, initializer lists are the only way to call a base class's constructor from a derived constructor.


So what does this mean?, like in what situation would this be useful?

Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by johnnyBravo
So what does this mean?, like in what situation would this be useful?

Thanks.

Initializing using a custom constructor.

class Base
{
public:
Base(int num) { /* Do whatever */ } // This should only be called once.
};

class Derived : public Base
{
public:
Derived(int num) : Base(num) { } // Calls base ctor with int value.
};

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!