Member function returning a static variable

If a member function that returns a member variable staticshould also be static?

For instance:

struct T {
   static int i;
   static int getNumber() {
       return i;
   }
};

Should it getNumberbe staticor not?

+5
source share
2 answers

Usually yes.

If a variable does not have any state for each instance, then what possible logic for each instance could function on it before returning it?

+7
source

It's not obligatory. you can write a member function that returns a static variable. You cannot go the other way around (write a static function that returns an instance variable).

, , , . ,

+1

All Articles