3

I'm doing C++ after along time , i had declared a static variable inside the class as private and as far i know the static variables are independent of objects and shared across the objects .If i try to print the static variable outside the class using a class name i get the compilation errors is this because the variable is private ? I did read that the static variables can be accessed just by the Class name and the scope resolution operator .

#include <iostream>

using namespace std;

class Sample{

    int val;
   static int value;
    public:


        Sample(int in);
        Sample();
        void setval(int in){

            val = in;
        }

        void printval ()const{

            cout << val<<endl;
        }

};

Sample::Sample(int in){

    val = in;
}

Sample::Sample(){

    val = 0;
}

int Sample::value = 34;
int main()
{

   const Sample obj(1);
   Sample obj2;

   obj2.printval();

   obj.printval();

  cout <<"static value = " << Sample::value;

   return 0;
}

Error

main.cpp:37:5: error: 'int Sample::value' is private                                                                                                                                                  
 int Sample::value = 34;                                                                                                                                                                              
     ^                                                                                                                                                                                                
main.cpp:49:39: error: within this context                                                                                                                                                            
   cout <<"static value = " << Sample::value; 
Santhosh Pai
  • 2,535
  • 8
  • 28
  • 49
  • 2
    `is this because the variable is private` yes, and the error message is telling you exactly that. The fact that a variable is static doesn't change the fact that it is still private. – SingerOfTheFall Nov 16 '16 at 13:28
  • 1
    If you don't want for the variable to be `private`, why have you declared it as `private`? – Algirdas Preidžius Nov 16 '16 at 13:28
  • 2
    This is what private means - it's not allowet to use it outside the class. See: http://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance – woockashek Nov 16 '16 at 13:29
  • you are also accessing the static variable inside the non static function.it will also give you error. – Varun Kumar May 16 '18 at 12:35

2 Answers2

6

as far i know the static variables are independent of objects and shared across the objects

That is correct. However, sharing variables across object instances and making variables accessible are independent of each other. There are four combinations of (shared, accessible) pairs. All four are available to you:

  • public static variable is shared and accessible outside the class
  • private static variable is shared, but not accessible outside the class
  • public non-static variable is not shared, but accessible outside the class
  • private non-static variable is neither shared nor accessible outside the class

Note that the way you deal with the private static value can be modeled after the way you work with non-static val, i.e. by giving your class users some public member-functions to work with the static variable:

class Sample {
...
public:
    static int getvalue() { return value; }
};

Now you can print it like this:

cout << "static value = " << Sample::getvalue();
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Private class members and methods are accessible only by the class's members and methods. This is true whether the class member is static, or not. This has no influence on the accessibility of the class member.

Note that a public class method has access to private class members, just like any other method, and this does not preclude a public class method from returning a pointer or a reference to the private class members. That's one option for you.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148