Define a const static object variable inside the class

前端 未结 1 589
北荒
北荒 2021-01-19 05:30

I need to create a static object inside a class definition. It is possible in Java, but in C++ I get an error:

../PlaceID.h:9:43: error: invalid use of incom         


        
相关标签:
1条回答
  • 2021-01-19 06:00

    You can't define the member variable because the class isn't fully defined yet. You have to do like this instead:

    class PlaceID {
    
    public:
    
        inline PlaceID(const std::string placeName):mPlaceName(placeName) {}
    
        const static PlaceID OUTSIDE;
    
    private:
        std::string mPlaceName;
    };
    
    const PlaceID PlaceID::OUTSIDE = PlaceID("");
    
    0 讨论(0)
提交回复
热议问题