C++ virtual override functions with same name

前端 未结 4 1897
情话喂你
情话喂你 2020-12-03 17:19

I have something like that (simplified)

class A
{
  public:
    virtual void Function () = 0;
};

class B
{
  public:
    virtual void Function () = 0;
};

c         


        
4条回答
  •  忘掉有多难
    2020-12-03 17:29

    If A and B are interfaces, then I would use virtual derivation to "join" them (make them overlap). If you need different implementations for your Function if called through a pointer to A or to B then I would strongly recommend to choose another design. That will hurt otherwise.

    Impl "derives from" A and B means Impl "is a" A and B. I suppose you do not mean it.

    Impl "implements interface" A and B means Impl "behaves like" A and B. then same interface should mean the same behavior.

    In both cases having a different behavior according to the type of pointer used would be "schizophrenic" and is for sure a situation to avoid.

提交回复
热议问题