Given a pointer to a C++ object, what is the prefe

2019-01-28 03:43发布

Say I have:

class A {
public:
    static void DoStuff();

    // ... more methods here ...
};

And later on I have a function that wants to call DoStuff:

B::SomeFunction(A* a_ptr) {

Is it better to say:

    a_ptr->DoStuff();
}

Or is the following better even though I have an instance pointer:

    A::DoStuff()
}

This is purely a matter of style, but I'd like to get some informed opinions before I make a decision.

7条回答
来,给爷笑一个
2楼-- · 2019-01-28 04:34

Although I agree that A::DoStuff() is clearer, and it's what I'd write myself, I can see an argument for going via the pointer, which is "suppose the class name changes". If class A becomes class B, then we only need to update the class name in one place (the pointer declaration) instead of two.

Just a thought...

查看更多
登录 后发表回答