Search code examples
c++indentation

How should I indent do nothing initialization list constructors?


Example: Thread::Thread:

class Thread
{
    Process * parent_;
    unsigned __int32 id_;
    void * entryPoint_;
public:
    Thread(Process * parent, unsigned __int32 id, void * entryPoint) : 
        parent_(parent),
        id_(id),
        entryPoint_(entryPoint)
    {
    }
    unsigned __int32 GetId() const
    {
        return id_;
    }
    void * GetEntryPointAddress() const
    {
        return entryPoint_;
    }
};

I can't seem to come up with a way of indenting things so that it doesn't look strange... and yet this is a common pattern. What are common ways of indenting this?


Solution

  • I always put empty blocks on a single line – i.e. { } (notice the space!).

    Furthermore, I usually put the colon and commas in front of the initialization list members instead of after – this makes adding members later on easier.

    Thread(Process * parent, unsigned __int32 id, void * entryPoint)
        : parent_(parent)
        , id_(id)
        , entryPoint_(entryPoint)
    { }
    

    (Edit: I no longer follow this style myself: I omit the space between braces nowadays.)