Static variables in static method in base class and inheritance

前端 未结 6 1074
南方客
南方客 2021-01-02 11:44

I have these C++ classes:

class Base
{
protected:
    static int method()
    {
        static int x = 0;
        return x++;
    }
};

class A : public Base         


        
6条回答
  •  情深已故
    2021-01-02 12:04

    The variable will be shared - it is per-function - in this case the function it belongs to is Base::method(). However if class Base was a template class you would get one instance of the variable for each instantiation (each unique set of actual template parameters) of class Base template - each instantiation is a new function.

提交回复
热议问题