2

I just noticed that we can access c++ static member function by member-selection operator (. or –>)

for example:

class StaticTest
{
private:
  int y;
  static int x;
public:
  StaticTest():y(100){

  }
  static int count()
  {
    return x;
  }
  int GetY(){return y;}
  void SetY(){
    y = this->count();                         //#1 accessing with -> operator 
  }
};

Here are how to use

  StaticTest test;
  printf_s("%d\n", StaticTest::count());      //#2
  printf_s("%d\n", test.GetY());
  printf_s("%d\n", test.count());             //#3 accessing with . operator 
  test.SetY();
  1. what is the use case of #1 and #3?
  2. what is the difference between #2 and #3?

Another style of #1 for accessing static member function in member function is

  void SetY(){
    y = count();                             //however, I regard it as 
  }                                          // StaticTest::count()

But now it looks more like this->count(). Is there any difference of two style calling?

Thanks

Chang
  • 3,953
  • 2
  • 30
  • 43

3 Answers3

2

Have a look at this question.

According to the standard (C++03, 9.4 static members):

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

So, when you already have an object and you're calling a static method on it, then there is no difference to using the class member access syntax.

If you however need to create the object first (be it by instantiating the object directly before, or by calling some function), then this creation process will of course take up a little extra time and memory. The this-Pointer, however, is never passed in to a static function, the call itself is always the same, no matter how it was written.

Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71
  • Does it affect name lookup at compile time? because given a instance or this pointer, it has different meaning comparing with "class::" – Chang Dec 27 '11 at 08:42
  • Good question. If, then probably only very little; it might be one less lookup in the symbol table when the class name is specified (since in the other case, for an object, the class of this particular object would have to be looked up first, and only then the class itself). But I'd be very surprised if that has any noticeable effects on compile times. – codeling Dec 27 '11 at 10:16
1

I'm a little confused about the answer to your first question, but with regards to your second question:

in #2 ( StaticTest::count() ), you used the count() method in a static manner, which means you called the function without creating a StaticTest object.

in #3 (test.count()), you created a StaticTest object called test, and called the method through the object (even though it was unnecessary.)

Functionally, there is no difference, but #2 is the preferred way of calling StaticMethods. Making an object when it is not needed is bad use of memory.

W.K.S
  • 9,787
  • 15
  • 75
  • 122
1

C++03, 9.4 static members

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

Yola
  • 18,496
  • 11
  • 65
  • 106