Hi,

Can private static data members of a class be accessed through non-static methods of that class? I'm asking this (dumb) question because on a C++ book I've read that a class's static data members can only be accessed through a static function, but when I tried doing it with a non-static method, it also worked (Visual C++ 2010). So can I access them or not?

Thanks,
Dean

Yes.

I think you need to take another look at the wording in your book, paying particularly close attention to where the "only" is placed in the sentence and what it's attached to grammatically.

Speaking in reference to static functions, it probably says something like:

  • "a static function does not have a this pointer so it can only access (or only has access to) static member variables"
  • "They may only directly refer to other static members of the class."
  • other similar

It most likely does NOT say that static member variables can only be accessed through static functions. If it truly does, it's a grammatical error. What book are you reading/using, and what page are you referring to? Please post a quote.

Edited 5 Years Ago by Fbody: n/a

The book I'm using is 'Object-Oriented Programming Using C++' Second Edition, by a certain Joyce Farrell (a book my father used for his advanced diploma), and she mentions this statement at the penultimate paragraph of page 178:

(athleticFee is a public static data member of a class named Student)

"In the program shown in Figure 5-20, the athleticFee field is public, which is why you can access it directly, as in Student::AthleticFee = 139.95;. If it were private, you would have to use a public function to access the value, as with any other private variable. Additionally, the function would have to be a static function."

But it doesn't matter now because that's all I really needed to know. I just wanted to see if it was a question of better practice rather than if it can be done or not.

Thanks,
Dean

Edited 5 Years Ago by deanus: n/a

There are several meanings for "static". There are 2 ways it can be used within a class definition. When it's used within a class definition, it changes the member's behavior, not it's accessibility. The net result for both uses is essentially the same, only 1 copy of the variable or function exists throughout the entire program. Static variables and functions are still accessible anywhere allowed by their assigned access level (private/protected/public). The difference is that a static function can not access non-static parts of the class.

I think that in this particular case, it's either a matter of better practice (from a logical/design perspective) or an author that doesn't know what they are talking about.

The method would not HAVE TO be a static member, but it would be a good idea for it to be static so that it can't mess around with object-specific (non-static) values.

This is an excerpt from Herbert Schildt's book "The Complete C++ Reference, Fourth Edition" (310-311):

When you precede a member variable's declaration with static, you are telling the compiler that only one copy of that variable will exist and that all objects of the class will share that variable. Unlike regular data members, individual copies of a static member variable are not made for each object. No matter how many objects of a class are created, only one copy of a static data member exists. Thus, all objects of that class use that same variable. ...
...
To understand the usage and effect of a static data member, consider this program:

#include <iostream>
using namespace std;

class shared {
  static int a;
  int b;
public:
  void set(int i, int j) {a=i; b=j;}
  void show();
};

int shared::a;  //define a

void shared::show() {
  cout << "This is static a: " << a;
  cout << "\nThis is non-static b: " << b;
  cout << "\n";
}

int main() {
  shared x, y;

  x.set(1, 1);  //set a to 1
  x.show();

  y.set(2, 2);  //change a to 2
  y.show();

  x.show(); /* Here, a has been changed for both x and y
               because a is shared by both objects. */

  return 0;
}

Edited 5 Years Ago by Fbody: n/a

Okidokey, thanks very much... I think I understand them now...

This question has already been answered. Start a new discussion instead.