vendredi 25 décembre 2020

Implementing an abstract class as a interface for other classes without vtable overhead

I want to make interface classes ForwardIterator, BiderctionalIterator and RandomAccessIterator, each one of them has some operators (all of them are pure virtual and not implemented). Now if I want to implement an iterator for a container, I just inherit from the right iterator and get the help of the compiler if I accidently forgot to implement some functions/operators. Doing it with pure virtual functions works perfectly but It has the overhead of vtable which is unnecessary since the all code needs can be defined in compile time.

template <typename T>
class ForwardIterator{
    virtual T operator++() = 0;
    virtual T operator++(int) = 0;
};

template <typename T>
class BidirectionalIterator: public ForwardIterator<T>{
    virtual T operator--() = 0;
    virtual T operator--(int) = 0;
};

template <typename T>
class RandomAccessIterator: public Bidirectional<T>{
    virtual T operator+(int) = 0;
    virtual T operator-(int) = 0;
};

// Custom Iterator Implementation

class MyCustomRandomAccessIterator: public RandomAccessIterator<T>{
    // get errors if I miss some function definitions.
    // but it has vtable !!!

};

Aucun commentaire:

Enregistrer un commentaire