c++ 类中对象计数的静态变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7781188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:28:51  来源:igfitidea点击:

Static variable for object count in c++ classes?

c++classstatic

提问by mowwwalker

I would like to have a static member variable keep track of the amount of objects that have been made. Like so:

我想有一个静态成员变量来跟踪已创建的对象数量。像这样:

class test{
    static int count = 0;
    public:
        test(){
            count++;

        }
}

That doesn't work because, according to VC++, a member with an in-class initializer must be constant. So I looked around and apparently you're supposed to do:

这并不工作,因为,根据VC ++, a member with an in-class initializer must be constant。所以我环顾四周,显然你应该这样做:

test::count = 0;

Which would be great, but I want count to be private.

这会很棒,但我希望 count 是私密的。

edit:Oh boy, I just realized I need to do:

编辑:哦,男孩,我刚刚意识到我需要做:

int test::count = 0;

I had seen something just do test::count = 0, so I assumed you wouldn't have to declare type again.

我已经看到了一些 just do test::count = 0,所以我认为你不必再次声明类型。

I'd like to know if there's a way to do this inside the class though.

我想知道是否有办法在课堂上做到这一点。

edit2:

编辑2:

What I'm using:

我在用什么:

class test{
    private:
        static int count;
    public:
        int getCount(){
            return count;
        }
        test(){
            count++;

        }
}
int test::count=0;

Now it's saying: 'test' followed by 'int' is illegal (did you forget a ';'?)

现在它说: 'test' followed by 'int' is illegal (did you forget a ';'?)

edit3:

编辑3:

Yup, forgot a semicolon after the class definition. I'm not used to having to do that.

是的,忘记了类定义后的分号。我不习惯这样做。

回答by Seth Carnegie

Put

static int count;

In your header in the class definition, and

在类定义的标题中,以及

int test::count = 0;

In the .cpp file. It will still be private (if you leave the declaration in the header in the private section of the class).

在 .cpp 文件中。它仍然是私有的(如果您在类的私有部分的标题中保留声明)。

The reason you need this is because static int countis a variable declaration, but you need the definition in a single source file so that the linker knows what memory location you're referring to when you use the name test::count.

您需要它的原因是因为它static int count是一个变量声明,但您需要在单个源文件中进行定义,以便链接器知道您在使用 name 时所指的内存位置test::count

回答by elxala

Initialization of static variable within a function is allowed so a solution can be something like this

允许在函数内初始化静态变量,因此解决方案可以是这样的

 class test
 {
    private:
    static int & getCount ()
    {
       static int theCount = 0;
       return theCount;
    }
    public:
    int totalCount ()
    {
       return getCount ();
    }

    test()
    {      
       getCount () ++;
    }
 };

in general using this technique it is possible to work around the C++ limitation regarding static member initialization in the declaration.

一般来说,使用这种技术可以解决 C++ 关于声明中静态成员初始化的限制。

回答by Ben Voigt

staticclass members must be defined (and potentially initialized) at namespace scope, member access rules do not apply.

static类成员必须在命名空间范围内定义(并可能初始化),成员访问规则不适用。