Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static member variable when declared private

When a static member variable is declared private in a class, how can it be defined?

Suppose i have the following class declaration

class static_demo
{
   private:
      static int a;

   public:
      static int b;

      void set(int x, int y)
      {
         a = x;
         b = y;
      }

      void show()
      {
         cout << "a = " << a << "\n";
         cout << "b = " << b << "\n";
      }
};

Then the following statement to define a will result in compilation error.

int static_demo::a;

So is it possible to have a static data member in the private section of class?

Adding full code as per Greg,

#include <iostream>

using namespace std;

class static_demo
{
   private:
      static int a;

   public:
      static int b;

      void set(int x, int y)
      {
         a = x;
         b = y;
      }
};

int static_demo::a;
int static_demo::b;

int main()
{
   static_demo::b = 10;
   static_demo::a = 20;

   return 0;
}

The compilation error is:

static_member_variable.cpp: In function `int main()':
static_member_variable.cpp:20: error: `int static_demo::a' is private
static_member_variable.cpp:26: error: within this context
like image 411
nitin_cherian Avatar asked Oct 22 '11 07:10

nitin_cherian


People also ask

Can static member variables be private?

Static member variables It is essentially a global variable, but its name is contained inside a class scope, so it goes with the class instead of being known everywhere in the program. Such a member variable can be made private to a class, meaning that only member functions can access it.

Is static variable public or private?

Static variables are created when the program starts and destroyed when the program stops. Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class. Default values are same as instance variables.

Can static member function private?

Making a function a static member of a class rather than a free function gives two advantages: It gives the function access to private and protected members of any object of the class, if the object is static or is passed to the function; It associates the function with the class in a similar way to a namespace.

Are private static variables global?

Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class.


1 Answers

When a static member variable is declared private in a class, how can it be defined?

In the very same way as you define a public static variable in your source(cpp) file.

int static_demo::a = 1;

Access specifiers will not give you error while defining the member. Access specifiers control the access of the member variables, defining a static variable is an excpetion which is allowed.

Compiles cleanly on Ideone here.

EDIT: To answer your Q after you posted code.
Your problem is not the definition of the static member. The error is because you are trying to access the private static member inside main. You cannot do that.

Private members of a class can only be accessed inside the class member functions, the same rule applies even to static members. To be able to modify/access your static members you will have to add a member function to your class and then modify/access the static member inside it.

like image 131
Alok Save Avatar answered Sep 19 '22 20:09

Alok Save