Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow other base class to implement a virtual function

Is it possible to do the following:
My base class has 3 pure virtual functions. My derived class implements 2 of these virtual functions and inherits from another class that implements the final 3rd virtual function.

My current code won't compile so I am thinking this is not valid? Although it would be great if I could somehow use this approach. Below is my practical application/use of this approach.

Any suggestions of a different approach I could use to achieve this functionality?

class ListBox
{
public:
    virtual void onScroll() = 0;
    virtual void foo() = 0;
    virtual void bar() = 0;
};

class DragScrollHandler
{
public:
    void onScroll()
    {
        ...
    }
};

class HorzListBox: public ListBox, public DragScrollHandler // could also do public HoverScrollHandler, etc.
{
public:
    void foo() override
    {
        printf("foo\n");
    }

    void bar() override
    {
        printf("foo\n");
    }

    HorzListBox()
        : ListBox(), DragScrollHandler()
    {

    }
};
like image 334
sazr Avatar asked Jun 13 '16 06:06

sazr


People also ask

Can a virtual function be implemented in a derived class?

Derived classes do not have to implement all virtual functions themselves. They only need to implement the pure ones. That means the Derived class in the question is correct.

Can virtual function be defined in base class?

Rules for Virtual Functions The prototype of virtual functions should be the same in the base as well as derived class. They are always defined in the base class and overridden in a derived class.

How do you implement the virtual function?

The virtual functions must be declared in the public section of the class. They cannot be static or friend function also cannot be the virtual function of another class. The virtual functions should be accessed using a pointer to achieve run time polymorphism.

At which conditions does virtual base class need to be implemented?

Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given class appearing in an inheritance hierarchy when using multiple inheritances. Need for Virtual Base Classes: Consider the situation where we have one class A .


1 Answers

ListBox and DragScrollHandler are a priori different concepts, so there is no kind of link across the multiple inheritance. I mean it is not automatic to set a link in between the virtual onScroll inherited in some way, and an implementation of onScroll on another unrelated (to the other) branch.

You can define it on HorzListBox to call the inherited implementation:

void onScroll() override {
   DragScrollHandler::onScroll();
}

This would fulfill the contract: implement the abstraction and use the inherited implementation.

But may be the best way (in your case) is to separate concepts and have a Scrollable:

class Scrollable {
public: 
  virtual void onScroll()=0;
};

class ListBox: virtual public Scrollable {
public:
  virtual void foo()=0;
};

class DragScrollHandler: virtual public Scrollable {
public:
  void onScroll() override {...}
};

class HorzListBox: public ListBox, public DragScrollHandler {
public:
  void foo() override {...}
};
like image 121
Jean-Baptiste Yunès Avatar answered Oct 20 '22 08:10

Jean-Baptiste Yunès