Are static variables in a base class shared by all derived classes?

前端 未结 7 1961
梦如初夏
梦如初夏 2020-11-28 04:56

If I have something like

class Base {
    static int staticVar;
}

class DerivedA : public Base {}
class DerivedB : public Base {}

Will bot

7条回答
  •  庸人自扰
    2020-11-28 05:52

    I know that this question has already been answered but I would like to provide a small example of inheritance with static members. This is a very nice way to demonstrate the usefulness as well as what is happening with the static variables and the respective constructors.

    FooBase.h

    #ifndef FOO_BASE_H
    #define FOO_BASE_H
    
    #include 
    
    class FooBase {
    protected:
        std::string _nameAndId;
    private:
        std::string _id;
        static int _baseCounter;
    
    public:
        std::string idOfBase();
        virtual std::string idOf() const = 0;
    
    protected:
        FooBase();    
    };
    
    #endif // !FOO_BASE_H
    

    FooBase.cpp

    #include "FooBase.h"
    #include 
    
    int FooBase::_baseCounter = 0;
    
    FooBase::FooBase() {
        _id = std::string( __FUNCTION__ ) + std::to_string( ++_baseCounter );
        std::cout << _id << std::endl;
    }
    
    std::string FooBase::idOfBase() {
        return _id;
    }
    
    std::string FooBase::idOf() const {
        return "";
    } // empty
    

    DerivedFoos.h

    #ifndef DERIVED_FOOS_H
    #define DERIVED_FOOS_H
    
    #include "FooBase.h"
    
    class DerivedA : public FooBase {
    private:    
        static int _derivedCounter;
    
    public:
        DerivedA();
    
        std::string idOf() const override;
    };
    
    class DerivedB : public FooBase {
    private:
        static int _derivedCounter;
    
    public:
        DerivedB();
    
        std::string idOf() const override;
    };
    
    #endif // !DERIVED_FOOS_H
    

    DerivedFoos.cpp

    #include "DerivedFoos.h"
    #include 
    
    int DerivedA::_derivedCounter = 0;
    int DerivedB::_derivedCounter = 0;
    
    DerivedA::DerivedA() : FooBase() {
        _nameAndId = std::string( __FUNCTION__ ) + std::to_string( ++DerivedA::_derivedCounter );
        std::cout << _nameAndId << std::endl;
    }
    
    std::string DerivedA::idOf() const {
        return _nameAndId;
    }    
    
    DerivedB::DerivedB() : FooBase() {
        _nameAndId = std::string( __FUNCTION__ ) + std::to_string( ++DerivedB::_derivedCounter );
        std::cout << _nameAndId << std::endl;
    }
    
    std::string DerivedB::idOf() const {
        return _nameAndId;
    }
    

    main.cpp

    #include "DerivedFoos.h"
    
    int main() {
        DerivedA a1;  
        DerivedA a2;
        DerivedB b1;
        DerivedB b2;
    
        system( "PAUSE" );
        return 0;
    }
    

    If __FUNCTION__ is not working for you in your constructors then you can use something similar that can replace it such as __PRETTY_FUNCTION__ or __func__, or manually type out each class's name :(.

提交回复
热议问题