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:10

Jon Skeet opened my eyes to why you should not call a static method through an instance pointer. His example is in Java, but the concept applies to C++, too:

Thread t = new Thread(...);
t.start();
t.sleep(1000); // Which thread does it look like this will affect?

As I commented when I first read his answer: "Until I read [Jon's post], I considered being able to call static methods through an instance reference a feature. Now I know better."

In short, call static methods using the class name, not an instance. In my opinion, it's more than a style issue - it can result in misleading, buggy code.

查看更多
Viruses.
3楼-- · 2019-01-28 04:17

It's better to call the static method by its name, not through an object, since it doesn't actually use that object at all. In Java, the same problem exists. A not-too-uncommon problem in Java is the following:

Thread t = getSomeOtherThread();
t.sleep(1000);

This compiles fine but is almost always an error -- Thread.sleep() is a static method that causes the current thread to sleep, not the thread being acted on as the code seems to imply.

查看更多
【Aperson】
4楼-- · 2019-01-28 04:18

I think I'd prefer "A::DoStuff()", as it's more clear that a static method is being called.

查看更多
Luminary・发光体
5楼-- · 2019-01-28 04:18

I personally prefer the A::DoStuff() convention because it's immediately clear to anyone reading the code that it's a call to a static member function.

查看更多
Deceive 欺骗
6楼-- · 2019-01-28 04:19

I've seen many questions in Java where if people called a static method using the syntax of calling an instance method through an object, and the object is actually a subclass of the variable type, people wonder why it doesn't call a static method of the same name in the subclass. The fact that they are calling it through an object makes them think that it is an instance method which can be overridden and it somehow does runtime dynamic lookup using the type of the object.

But of course the object is never used in the call at all -- only the type of the variable is used at compile-time to decide what class's static method it is. So if you put an object there, it is completely misleading because it makes people think that it is used, when it is not. That's why I favor only calling static methods through the class name -- it is exactly equivalent to calling it through a variable of the class type; but it tells you exactly what is going on, with no useless misleading extra information.

查看更多
ら.Afraid
7楼-- · 2019-01-28 04:25

Generally I do the A::DoStuff(); way instead of a->DoStuff(); because maybe someday the function I'm in won't have that instance pointer anymore due to refactoring. But it's a total style thing that you shouldn't loose any sleep over.

查看更多
登录 后发表回答