Whats this strange code...c++

Started by
24 comments, last by me22 18 years, 4 months ago
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
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
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Ah I see thanks. I guess I'll start doing that then.
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).
Also, initializer lists are the only way to call a base class's constructor from a derived constructor.
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.}
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.
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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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.
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.};

This topic is closed to new replies.

Advertisement