代码之家  ›  专栏  ›  技术社区  ›  sorush-r

如何访问类的静态成员?

  •  22
  • sorush-r  · 技术社区  · 13 年前

    我开始学习C++和QT,但有时我从书中粘贴的最简单的代码会导致错误。

    我在用 g++4.4.2 在带有QtCreator IDE的Ubuntu 10.04上。g++编译器语法和其他编译器有什么不同吗?例如,当我试图访问静态成员时,总会出现问题。

    #include <iostream>
    using namespace std;
    class A
    {
       public:
          static int x;
          static int getX() {return x;}
    };
    int main()
    {
       int A::x = 100; // error: invalid use of qualified-name 'A::x'
       cout<<A::getX(); // error: : undefined reference to 'A::x'
       return 0;
    }
    

    here 在这里

    8 回复  |  直到 6 年前
        1
  •  42
  •   SeasonalShot    8 年前

    你已经声明静态成员没问题,但是没有 defined 他们在任何地方。

    基本上,你所说的“存在一些静态成员”,但永远不要为它留出一些记忆,你需要:

    int A::x = 100;
    

    在主管道内。

        2
  •  9
  •   Prasoon Saurav    13 年前

    第[9.4.2]节

    静态数据成员在其类定义中的声明不是定义,并且可能是不完整的类型,而不是cv限定的void。 静态数据成员的定义应出现在包含成员类定义的命名空间范围中 . 在命名空间作用域的定义中,静态数据成员的名称应使用 :: 操作人员

        3
  •  7
  •   Loki Astari    13 年前

    #include <iostream>
    using namespace std;
    class A
    {
       public:
          // This declares it.
          static int x;
          static int getX(){return x;}
    };
    
    // Now you need an create the object so
    // This must be done in once source file (at file scope level)
    int A::x = 100;
    
    
    int main()
    {
       A::x = 200;
       // Note no int here. You can modify it
    
       cout<<A::getX(); // Should work
       return 0;
    }
    
        4
  •  5
  •   Shubham Singh    6 年前

    您需要在类外部定义类的静态成员变量,因为静态成员变量需要声明和定义。

    #include <iostream>
    using namespace std;
    class A
    {
       public:
          static int x;
          static int getX() {return x;}
    };
    
    int A::x;         // STATIC MEMBER VARIABLE x DEFINITION
    
    int main()
    {
       A::x = 100;    // REMOVE int FROM HERE
       cout<<A::getX();
       return 0;
    }
    
        5
  •  3
  •   Cullub    9 年前

    试试这个例子:

    #include<iostream>
    using namespace std;
    
    class check
    {
            static int a;
        public:
            void change();
    } ;
    int check::a=10;
    void check::change()
    {
        a++;
        cout<<a<<"\n";
    }
    
    int main()
    {
    
        int i,j;
        check c;
        check b;
        c.change();
        b.change();
        return 0;
    }
    
        6
  •  2
  •   Oliver Charlesworth    13 年前

    静态成员变量的定义必须存在于文件范围内,即在所有函数等外部。

        7
  •  1
  •   thkala jaxb    12 年前

    现在您已经了解了如何使用静态类成员,我建议您通常只应在以下情况下使用它们:

    • template< typename T >
      int func()
      {
          return T::GetX();
      }
      

      尽管显然更为复杂。但是在这里,你的静态函数在一个类中是有目的的。

    • 函数需要访问类的地方,即访问私有成员的地方。你可以让它成为朋友,但你也可以让它静止。通常是回拨。

    剩下的时间,您可能可以使用编译单元级的函数和变量,这些函数和变量的优点是将您的成员从头中取出(特别是如果它们是私有的)。实现细节越少越好。

        8
  •  0
  •   Eswaran Pandi    7 年前

    案例1: 静态变量

    众所周知,在类中定义一个静态变量会引发编译错误。E、 g.以下

    class Stats
    {
      public: 
         static int AtkStats[3];
         *static int a =20;*        // Error: defining a value for static variable
    };
    
    int Stats::AtkStats[3] =  {10, 0, 0};
    

    error: ISO C++ forbids in-class initialization of non-const static member 'Stats::a'
    

    案例2: 常量静态变量

    常量 静态变量,我们可以在类内或类外定义一个值。

    class Stats
    {
      public: 
         static const int AtkStats[3];
         static const int a =20;        // Success: defining a value for a const static
    };
    
    const int Stats::AtkStats[3] =  {10, 0, 0};
    
    const int Stats::a = 20;        // we can define outside also
    

    Compilation success.