C++ abstract class without pure virtual functions?

前端 未结 3 538
Happy的楠姐
Happy的楠姐 2020-12-01 15:44

I have a base class

class ShapeF
{
public:
    ShapeF();
    virtual ~ShapeF();

    inline void SetPosition(const Vector2& inPosition) { mPosition.Set(i         


        
相关标签:
3条回答
  • 2020-12-01 16:25

    You could declare, and implement, a pure virtual destructor:

    class ShapeF
    {
    public:
        virtual ~ShapeF() = 0;
        ...
    };
    
    ShapeF::~ShapeF() {}
    

    It's a tiny step from what you already have, and will prevent ShapeF from being instantiated directly. The derived classes won't need to change.

    0 讨论(0)
  • 2020-12-01 16:26

    Try using a protected constructor

    0 讨论(0)
  • 2020-12-01 16:36

    If your compiler is Visual C++ then there is also an "abstract" keyword:

    class MyClass abstract
    {
        // whatever...
    };
    

    Though AFAIK it will not compile on other compilers, it's one of Microsoft custom keywords.

    0 讨论(0)
提交回复
热议问题