Using base class operators for derived classes in C++11

mic_e

Consider the example.

I have a container class (A), which overloads/implements all kinds of arithmetic operators (A::negate()).

I now wish to create derived classes (B and C).

B and C should have all operators implemented by A.

However, those operators should use derived-class objects as arguments.

The prototype for B::negate should be: B B::negate(), instead of the A B::negate().

The derived classes do not need any own fields, but may implement own methods (B::foo(), C::bar()). It is a requirement that B and C be incompatible, i.e., a B object can not be assigned to a C object, or used with any of C's operators.

Here is the example code, how I want it to work:

struct A {
        int val;

        A negate() {
                return A{-val};
        }
};

struct B: A {void foo(){}};
struct C: A {void bar(){}};

int main() {
        B obj0 = {5};
        B obj1 = obj0.negate();
}

I understand that this is probably impossible using standard inheritance, and might be something C++11 simply isn't capable of, so I'm asking for something as close as possible to it.

The currently best solution I've come up with involves not using inheritance at all, but instead adding an integer template parameter to the base class, defining derived classes as using B = A<1>;,using C = A<2>;, and implementing member methods only for some specializations (only A::foo<1>(){} and A::bar<2>(){}).

However, I'm highly unhappy with this solution.

Yakk - Adam Nevraumont
template<typename Child>
struct A {
  Child* self() {
    static_assert( std::is_base_of< A<Child>, Child >::value, "CRTP failure" );
    return static_cast<Child*>(this);
  }
  Child const* self() const {
    static_assert( std::is_base_of< A<Child>, Child >::value, "CRTP failure" );
    return static_cast<Child const*>(this);
  }
  Child negate() {
    return Child{-val};
  }
};
struct B:A<B> {
  explicit B(int v):A<B>(v) {}
};

here, we inject information into the base class template about its child. B is then relatively free to be a normal class.

In the parent, you can get self() in order to access your this pointer as a B (or other derived class).

Another approach involves free functions. You write a free negate template function that checks if its argument is derived from A, and if so does the negate action, and returns the negative version of the type passed in.

A mixture of these also works, where your free function takes A<D>s and returns a D.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Instantiating objects in an array using base class and derived classes in C#

From Dev

How to overload operator << for derived classes using a shared base class?

From Dev

Derived classes not using all pure virtual methods from the base class

From Dev

C#: Define methods implementation in base class and properties in derived classes

From Dev

C#: Define methods implementation in base class and properties in derived classes

From Dev

C++ Derived classes only using part of inherited base constructor

From Dev

C++ Using two variables of same name in Derived and Base classes

From Dev

C++ using static array in base class, declaring in derived class

From Dev

How to serialize the base class with derived classes

From Dev

Base Class Enum Implemented Differently in Derived Classes

From Dev

Storing derived classes in a base class Dictionary

From Dev

Base class of non polymorphic derived classes

From Dev

Are base classes in inheritance copied to the derived class?

From Dev

Are base classes in inheritance copied to the derived class?

From Dev

Index all derived classes of base class ravendb

From Dev

Factory method on base class to instantiate derived classes

From Dev

Using Derived Class In a Base Method

From Dev

Thread base class using C++11

From Dev

using Static Container for base and derived classes

From Dev

Overriding << operator in C++11, pure virtual in base class and different implementations in each derived class

From Dev

C++11: Can I explicitly call a base class destructor to destroy the derived class?

From Dev

Scope with Base and Derived Classes in C++

From Dev

Python: Using derived class attributes in base class

From Dev

EF6 - TPH foreign key mapping in derived classes using base class property

From Dev

EF6 - TPH foreign key mapping in derived classes using base class property

From Dev

C++ -- Assigning a derived class to a base class

From Dev

Upcasting derived class to base class in C#

From Dev

C++ Should common data fields in derived classes be declared in base class?

From Dev

C++,Strategy to use base class pointer storing subclasses and use the derived classes' method

Related Related

  1. 1

    Instantiating objects in an array using base class and derived classes in C#

  2. 2

    How to overload operator << for derived classes using a shared base class?

  3. 3

    Derived classes not using all pure virtual methods from the base class

  4. 4

    C#: Define methods implementation in base class and properties in derived classes

  5. 5

    C#: Define methods implementation in base class and properties in derived classes

  6. 6

    C++ Derived classes only using part of inherited base constructor

  7. 7

    C++ Using two variables of same name in Derived and Base classes

  8. 8

    C++ using static array in base class, declaring in derived class

  9. 9

    How to serialize the base class with derived classes

  10. 10

    Base Class Enum Implemented Differently in Derived Classes

  11. 11

    Storing derived classes in a base class Dictionary

  12. 12

    Base class of non polymorphic derived classes

  13. 13

    Are base classes in inheritance copied to the derived class?

  14. 14

    Are base classes in inheritance copied to the derived class?

  15. 15

    Index all derived classes of base class ravendb

  16. 16

    Factory method on base class to instantiate derived classes

  17. 17

    Using Derived Class In a Base Method

  18. 18

    Thread base class using C++11

  19. 19

    using Static Container for base and derived classes

  20. 20

    Overriding << operator in C++11, pure virtual in base class and different implementations in each derived class

  21. 21

    C++11: Can I explicitly call a base class destructor to destroy the derived class?

  22. 22

    Scope with Base and Derived Classes in C++

  23. 23

    Python: Using derived class attributes in base class

  24. 24

    EF6 - TPH foreign key mapping in derived classes using base class property

  25. 25

    EF6 - TPH foreign key mapping in derived classes using base class property

  26. 26

    C++ -- Assigning a derived class to a base class

  27. 27

    Upcasting derived class to base class in C#

  28. 28

    C++ Should common data fields in derived classes be declared in base class?

  29. 29

    C++,Strategy to use base class pointer storing subclasses and use the derived classes' method

HotTag

Archive