i_am_jorf
i_am_jorf

Reputation: 54600

Given a pointer to a C++ object, what is the preferred way to call a static member function?

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.

Upvotes: 5

Views: 2018

Answers (7)

newacct
newacct

Reputation: 122449

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.

Upvotes: 0

Michael Burr
Michael Burr

Reputation: 340218

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.

Upvotes: 1

Brian Neal
Brian Neal

Reputation: 32389

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.

Upvotes: 0

anon
anon

Reputation:

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

Upvotes: 3

Adam Rosenfield
Adam Rosenfield

Reputation: 400284

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.

Upvotes: 19

Ben Collins
Ben Collins

Reputation: 20686

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.

Upvotes: 6

Fred Larson
Fred Larson

Reputation: 62073

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

Upvotes: 22

Related Questions