статическая переменная-член при объявлении частной

Когда статическая переменная-член объявляется частной в классе, как ее можно определить?

Предположим, у меня есть следующее объявление класса

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";
      }
};

. Тогда следующий оператор для определения a приведет к ошибке компиляции.

int static_demo::a;

Возможно ли иметь статический элемент данных в закрытом разделе класса?

Добавление полного кода согласно Грегу ,

#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;
}

Ошибка компиляции:

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
15
задан nitin_cherian 22 October 2011 в 07:38
поделиться