19

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
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
  • 1
    possible duplicate of [Initializing private static members](http://stackoverflow.com/questions/185844/initializing-private-static-members) – KV Prajapati Oct 22 '11 at 07:34
  • Does this answer your question? [How to initialize private static members in C++?](https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c) – Marine Galantin Feb 12 '22 at 17:20

3 Answers3

31

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.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • It’s not exactly an exception: it’s a definition of *a* member of the class, so it has access to *all* members of that class (including the one being defined), just as much as in the body of a member function defined outside the class. Note that its type can be named using a private member even though it comes before the declarator entirely. – Davis Herring Jan 07 '22 at 02:13
3

in your .cpp:

int static_demo::a(0);

when this causes an error, it is most common that you either encountered a linker error for a multiply defined symbol (e.g. the definition was in the header), or that you may have tried to define it in the wrong scope. that is, if static_demo is in the namespace MON, it would be defined in the .cpp:

#include "static_demo.hpp"
int MON::static_demo::a(0);

for other types, it may simply have been an incorrect constructor.

justin
  • 104,054
  • 14
  • 179
  • 226
0

The problem is not the definition, but the fact that in main() (that's not in the name scope of static_demo, and cannot see a being private), you do an assignment.

The definition of a is what you did at global scope, with int static_demo::a;. You simply need an initializer, if you want a not to start with an undefined value.

int static_demo::a = 1;

will solve the problem.

From than on, a can be manipulated only by functions in static_demo (of friend of it).

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
  • 1
    `From than on, a can be manipulated only by static functions in static_demo (of friend of it).` is not correct.static members can also be accessed in non static member functions.check the [demo](http://www.ideone.com/SMyVt). – Alok Save Oct 22 '11 at 07:53