Reading List

The Selfish Gene
The Psychopath Test: A Journey Through the Madness Industry
Bad Science
The Feynman Lectures on Physics
The Theory of Everything: The Origin and Fate of the Universe


ifknot's favorite books »

Friday, 1 August 2014

Stuff about C++11 that I like... #2 Delegating Constructors

Delegating constructors is cool.

TL;DR With C++11 you can kiss goodbye to some 'init' method function shared between constructors or copying and pasting between your constructors.


Like inherited constructors I like delegating constructors[1] too - it's intuitive and clear:

template<typename T>
class worker {
public:
enum states {WAITING, RUNNABLE, STEALING, TERMINATED};
// default runnable start state is C++11 delegated to the general purpose constructor
worker(std::vector<T>& queues): worker(queues, RUNNABLE) {}
worker(std::vector<T>& queues, states state): this_id(count++), state(state), queues(queues) {
//do shared intialization stuff
}
.
.
.
view raw delegate.cpp hosted with ❤ by GitHub

References:

No comments:

Post a Comment