Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing private static variable outside class

Tags:

c++

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; 
like image 811
Santhosh Pai Avatar asked Jan 17 '26 23:01

Santhosh Pai


1 Answers

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();
like image 164
Sergey Kalinichenko Avatar answered Jan 19 '26 14:01

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!