Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ calling a static method

Let's say you have the following class:

struct Foo {        
    static void print(std::string x) { std::cout << x << std::endl; }       
};

What is the difference between calling print like

Foo foo; //Or a pointer...
foo.print("Hello world");

and

Foo::print("Hello world");

?

like image 758
Arrrow Avatar asked Oct 03 '17 12:10

Arrrow


People also ask

How can I call static method?

A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.

Are there static methods in C?

In C, functions are global by default. The “static” keyword before a function name makes it static. For example, below function fun() is static.

Can I call a static function from another file in C?

Functions in C are global by default. To make them local to the file they are created, we use the keyword static before the function. Static functions can't be called from any other file except the file in which it is created. Using static function, we can reuse the same function name in different files.

How do you call a static class?

Static methods can be called without creating an object. You cannot call static methods using an object of the non-static class. The static methods can only call other static methods and access static members. You cannot access non-static members of the class in the static methods.


2 Answers

There's the obvious difference in that the first version has to construct and destruct a Foo.

Then there's the obvious similarity in that both versions do the same thing when the function call is executed (construct a string, print, etc).

The less obvious difference, is in the evaluation of the two expressions. You see, even though foo is not required for the call, it's still evaluated as part of the expression:

[class.static]/1

A static member s of class X may be referred to using the qualified-id expression X​::​s; it is not necessary to use the class member access syntax to refer to a static member. A static member may be referred to using the class member access syntax, in which case the object expression is evaluated.

In your case that doesn't mean anything. But in certain contexts, it can prevent your program from compiling at all. For instance, if foo was instead a reference parameter in a constexpr function.

like image 53
StoryTeller - Unslander Monica Avatar answered Sep 19 '22 17:09

StoryTeller - Unslander Monica


There's no difference at the point of calling. It's six of one and half a dozen of another.

Foo::print("Hello world"); is more idiomatic; a convention has grown up where this signals to the reader that print is likely to be a static function. To that end, using foo.print("Hello world"); in your particular case is idiosyncratic, and therefore confusing. So avoid this way, particularly if there's an overhead in introducing an unnecessary instance foo.

Note that the notation using the scope resolution operator can also be used if you want to reach a specific override of a print within another method in a complex class hierarchy! Hence my use of likely above.

like image 38
Bathsheba Avatar answered Sep 21 '22 17:09

Bathsheba