When is a non-static const data member more useful than a const static one?

前端 未结 2 1428
一个人的身影
一个人的身影 2021-01-21 03:49

In C++ there are static and non-static const data members. When I want a constant, I always make it static because it does not make sense to have multi

2条回答
  •  走了就别回头了
    2021-01-21 04:18

    1. A static const applies to the class
    2. A const applies to the object

    E.g.

    class Verticies {
        public:
           const int colour;
           static const int MAX_VERTICIES = 100;
           Point points[MAX_VERTICIES];
           Verticies(int c) : colour(c) { }
           // Etc
    };
    

    Here MAX_VERTICIES applies to all objects of type Verticies. But different objects could have different colours and that colour is fixed on construction

提交回复
热议问题