开发者

How should I indent do nothing initialization list constructors? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 7 years ago.

Improve this question

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 loo开发者_开发知识库k strange... and yet this is a common pattern. What are common ways of indenting this?


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.)


This is how I do it:

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

But your way does not look strange to me.


Here's how I do it

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

Google style (atleast protobuf), would be:

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


Here's how I do it, and why I don't see anything wrong with your sample:

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

As far as I'm concerned, do it your way: As long as you are self-consistent and consistent with the project you're working on, it doesn't matter what your indentation style is.


I'd recommend putting a comment into the empty constructor body, just so anyone reading the code knows that you intended it to be empty. That way they can be certain that it's not a case of you having forgotten to insert code there.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜