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

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.

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.

Covariant return types:
#include <iostream>
struct A {
virtual A& operator ! () { std::cout << "A" << std::endl; return *this; }
};
struct B : public A {
virtual B& operator ! () { std::cout << "B" << std::endl; return *this; }
};
int main() {
B b;
A* a = &b;
! *a;
}

If you don't want to use templates:
Make A::negate() protected.
In B:
struct B : public A
{
B & negate()
{
A:negate();
return *this
}
/// and so on
}
Because negate is defined in B, it totally hides the negate defined in A so the B implementation gets called and can delegate to A.
If you plan to hide ALL of A from the user, then B should contain an A rather than inheriting from it. (make A::negate public again)
struct B
{
private:
A m_a;
public:
B & negate()
{
m_a.negate();
return *this
}
/// and so on
}

Related

Upcast to abstract base class

I try to have an abstract base class act like an interface and instantiate a derived class based on user input. I tried to implement it like this
class A
{
public:
virtual void print() = 0;
};
class B : A
{
public:
void print() override { cout << "foo"; }
};
class C : A
{
public:
void print() override { cout << "bar"; }
};
int main()
{
bool q = getUserInput();
A a = q ? B() : C();
a.print();
}
But that does not work. I’m coming from c# and that would be valid c# so I’m looking for an equivalent way of implement it in c++. Could someone please give me a hint? Thanks!
There are two problems with the code in its current state.
By default in C++, a class that inherits from a class or struct will be private inheritance. E.g. when you say class B : A, it's the same as writing class B : private A -- which in C++ restricts the visibility of this relationship only to B and A.
This is important because it means that you simply cannot upcast to an A from outside the context of these classes.
You are trying to upcast an object rather than a pointer or reference to an object. This fundamentally cannot work with abstract classes and will yield a compile-error even if the code was well formed.
If the base class weren't abstract, then this would succeed -- but would perform object slicing which prevents the virtual dispatch that you would expect (e.g. it won't behave polymorphically, and any data from the derived class is not present in the base class).
To fix this, you need to change the inheritance to explicitly be public, and you should be using either pointers or references for the dynamic dispatch. For example:
class A
{
public:
virtual void print() = 0;
};
class B : public A
// ^~~~~~
{
public:
void print() override { cout << "foo"; }
};
class C : public A
// ^~~~~~
{
public:
void print() override { cout << "bar"; }
};
To model something closer to the likes of C#, you will want to construct a new object. With the change above to public, it should be possible to use std::unique_ptr (for unique ownership) or std::shared_ptr (for shared ownership).
After this, you can simply do:
int main() {
auto a = std::unique_ptr<A>{nullptr};
auto q = getUserInput();
if (q) { // Note: ternary doesn't work here
a = std::make_unique<B>();
} else {
a = std::make_unique<C>();
}
}
However, note that when owning pointers from an abstract base class, you will always want to have a virtual destructor -- otherwise you may incur a memory leak:
class A {
public:
...
virtual ~A() = default;
};
You can also do something similar with references if you don't want to use heap memory -- at which point the semantics will change a little bit.
References in C++ can only refer to an object that already has a lifetime (e.g. has been constructed), and can't refer to a temporary. This means that you'd have to have instances of B and C to choose from, such as:
int main() {
auto b = B{};
auto c = C{};
bool q = getUserInput();
A& a = q ? b : c;
a.print(); // A& references either 'b' or 'c'
}
You need to use pointers :
A* a = q ? static_cast<A*>(new B) : new C;
...
delete a;
A ternary is also a special case here, as it takes the type of the first expression.
If C inherited from B here it would not be a problem.

Changing member types in inheritance

Given a base class Base that has two derived classes, DerA and DerB, can the derived classes have a member variable that is used in a Base member function, but is a different type for each class?
class Base {
* a // Declare a as *something*, so it can be used by "doWork"
template <typedef T>
void doWork(T b) { // Add another value to "a" which is of its same type
a += b; // For example; an operation that works on "a", no matter what numeric type it is
}
}
class DerA : public Base {
// Make "a" an int
}
class DerB : public Base {
// Make "a" a float
}
In practice, a will be a base struct, while DerA and DerB will have derived versions of the base struct (derivative classes will each have a derived form of the struct specific to their purpose, but each must do a simple operation on a, so it seems pointless to copy/paste that simple function for each derivative when I can just use a template function). I would just type a as the base struct type, but then I lose access to the various specialized member functions and variables that each derived struct has (if I understand inheritance correctly).
I apologize if this question is a repeat, but I don't know what this quality would be called, so Googling proved fruitless.
What you might want is the CRTP.
template<class D>
struct Base {
D* self() { return static_cast<D*>(this); }
D const* self() const { return static_cast<D*>(this); }
template<class T>
void doWork(T b) {
self()->a += b;
}
};
struct DerA : public Base<DerA> {
int a;
};
struct DerB : public Base<DerB> {
double a;
};
Here we pass the derived type to our base class. Within the base class, you can use self()-> to access fields in the derived type. This allows basically full access to the derived type, while letting us share code in the base class.
Note that you cannot pass DerA and DerB around as a Base this way. If you want that, you need a virtual method doWork, and virtual template methods don't exist.
CRTP stands for the curiously repeating template pattern, which I imagine is named because it is strange, it involves repeating a type, and it keeps on showing up in strange corners as being useful.
Type erasure probably won't work either, as you want to dispatch the type erasure from two different spots in the code base (the double dispatch problem: you need a centralized list of types supported to do the type Cartesian product on).
To expand on that, in order to support a+=b where both a and b are arbitrary types, you would have to expand over all types twice over, including types that are never mutually visible at the same spot in a compilation unit. That isn't possible.
If you need a common base, and there are only some types you pass to doWork, here is how you do it:
struct Base {
virtual void doWork( double ) = 0;
virtual void doWork( int ) = 0;
virtual void doWork( long long ) = 0;
};
template<class D>
struct Base_helper:Base {
D* self() { return static_cast<D*>(this); }
D const* self() const { return static_cast<D*>(this); }
template<class T>
void doWork_impl(T b) {
self()->a += b;
}
void doWork( double x ) override { doWork_impl(x); };
void doWork( int x ) override { doWork_impl(x); };
void doWork( long long x ) override { doWork_impl(x); };
};
struct DerA : public Base_helper<DerA> {
int a;
};
struct DerB : public Base_helper<DerB> {
double a;
};
note that every version of doWork must be valid to call on each of the Ders, as the Base_helper instantiates all of them.
If the kind of type passed to doWork is unbounded, yet the types of Der is bounded, you can do something like the above only backwards. It gets awkward, however. Your best bet in that kind of situation is to use a boost::variant type solution.
I guess you want to achieve something like this:
template<typedef T>
class Base {
T a;
void doWork(T b) { // Add another value to "a" which is of its same type
a += b; // For example; an operation that works on "a", no matter what numeric type it is
}
}
class DerA : public Base<int> {
}
class DerB : public Base<float> {
}
Or you can dump classes DerA and DerB entirely and use typedefs instead:
typedef Base<int> DerA;
typedef Base<float> DerB;
This can be easily solved with a CRTP-like pattern:
template<class D> // Base class is templated
class Base {
public:
D a;
void doWork(D b) {
a += b;
}
};
class DerA : public Base<int> {};
class DerB : public Base<float> {};
Live Example
Edit: in case you need only one common base (Base<int> is a completely different type from Base<float>) you might use an interface class and have Base inherit from it.

Downcasting instance of base class to derived class

I am interested if it is safe, to DOWNCAST (thanks Mike) an instance of a base class to a derived class under certain conditions. I think a sample is the most easy way to explain:
struct BaseA
{
void foo() const {}
double bar_member;
// no virtuals here
};
struct DerivedA : public BaseA
{
double bar(double z) {bar_member = z;return bar_member;}
// DerivedA does not add ANY member variables to BaseA.
// It also does not introduce ANY virtual functions.
};
struct BaseB
{
BaseA baseA;
};
// add extra functionality to B, to do this,
// i also need more functionality on baseA.
struct DerivedB : public BaseB
{
// is this "safe"? since BaseA and DerivedA
// should have the same memory layout?!?
DerivedA& getA() {return *static_cast<DerivedA*>(&baseA);}
double foo(double z) {return getA().bar(z);}
};
#include <iostream>
int main(int argc, char** argv)
{
DerivedB b;
// compiles and prints expected result
std::cout << b.foo(argc) << std::endl;
}
In my case, the classes BaseA and BaseB implement some kind of view concept. However, they also hold all the data members required to add further functionality in the derived classes. I know that I could implement the view to hold only a reference to the class providing the functionality. However, that would comes with some drawbacks:
I need to rewrite the whole interface for the view classes.
In my case, the Derived classes possesses an extra template argument (a callback type), which I want to have erased in the view. Hence, the view must not hold a direct reference to the classes providing functionality.
I tested my code, it works, however, I don't really trust the approach. And yes, I know I could achieve some of this with virtuals etc. but it is really performance critical...
Any ideas, hints, are welcome
Martin
for the interested people:
i changed my design the following way:
struct DerivedB : public BaseB
{
// encapsule the required extended functionality of BaseA member
struct OperateOnBaseA
{
OperateOnBaseA(BaseA& a);
double dosomething(double);
};
OperateOnBaseA a_extension;
DerivedB() :a_extension(baseA) {}
double foo(double z) {return a_extension.dosomething();}
};
As to the technical side: It is of course forbidden by the 2011 standard, 5.2.9.11, static cast. Let B be a base of D:
If the prvalue of type “pointer to cv1 B” points to a B that is actually a subobject of an object of type D, the resulting pointer points to the enclosing object of type D. Otherwise, the result of the cast is undefined.
On the other hand I'd be surprised if somebody could find an implementation which doesn't just do it, because of the obvious implementations of classes, methods and static casts.
Your existing code has undefined behaviour, as stated in the other answers. You can avoid that, if you don't mind some truly horrible code, by destroying the object at baseA and creating a DerivedA at the same location, so the downcast is valid:
#include <new>
struct DerivedB : public BaseB
{
DerivedB()
{
static_assert( sizeof(BaseA) == sizeof(DerivedA), "same size" );
baseA.~BaseA();
::new(&baseA) DerivedA();
}
~DerivedB()
{
getA().~DerivedA();
::new(&baseA) BaseA();
}
DerivedA& getA() {return *static_cast<DerivedA*>(&baseA);}
double foo(double z) {return getA().bar(z);}
};
The destructor restores an object of the original type, so that when the BaseB destructor destroys its baseA member it runs the destructor of the correct type on the object.
But I would avoid doing this and redesign your classes to solve it another way.
I don't find your approad clean enough for what you're trying to do. Assuming there's a "data source type" SourceA and a "data view type" ViewB, I would go more like this:
#include <iostream>
using namespace std;
template<typename T>
class SourceA_base
{
protected:
T data;
public:
using value_type = T;
SourceA_base(T&& a) : data(std::move(a)) { }
SourceA_base(T const& a) : data() { }
void foo() const {}
};
template<typename T>
class SourceA : public SourceA_base<T>
{
using B = SourceA_base<T>;
public:
using B::B;
T bar(T z) { return B::data = z; }
};
template<typename U>
class ViewB_base
{
protected:
U&& source;
public:
using value_type = typename std::remove_reference<U>::type::value_type;
ViewB_base(U&& a) : source(std::forward<U>(a)) { }
};
template<typename U>
class ViewB : public ViewB_base<U>
{
using B = ViewB_base<U>;
using T = typename B::value_type;
public:
using B::B;
T foo(T z) { return B::source.bar(z); }
};
int main ()
{
using S = SourceA<double>;
S a{3.14};
ViewB<S&> b{a};
std::cout << b.foo(6.28) << std::endl; // compiles and prints expected result
std::cout << ViewB<S>{S{2}}.foo(4) << std::endl; // still works
}
That is, all (source/view) types are templated, views contain references, and there are no downcasts. On your reservations for the use of references:
Re-writing the whole interface: No need now, thanks to templates.
Erasing callback types: First, type erasure and performance critical applications are not always good friends. Second, you'd better have the callback erase its own underlying type(s), not the view erase the type of the callback. Each class should do its own job. Or, don't erase types and make them template parameters.
I used rvalue-references so that the whole thing works for temporaries as well, as shown in my second example. Maybe constructors are not always complete/correct here. e.g. for const references; I reality would have fully templated constructors (accepting universal references), but to make this cooperate with one-argument implicitly defined copy/move constructors is a bit trickier (needs type traits and enable_if) and I only wanted to hightlight the idea here.
You may also consider using tuples to hold data, taking advantage of their empty base optimization.
As for your original question, this downcast is something I would never do; for the technical side, see Peter Schneider's answer.

Inheritance and polymorphism using abstract class

I have an abstract type A, and two derived types A1 and A2.
I want to add a method M to class A, which takes a parameter of type A. But, I need ad hoc polymorphism.
Indeed, I need 3 implementations : A1::M(A1 a), A1::M(A2 a) and A2::(A1 a), A2::M(A2 a).
But I want an abstract way to call the method M with pointers of type A.
I could put all signatures declaration in class A, but it sucks.
Use simulated double dispatch.
class A {
public:
virtual void M(A &) = 0;
virtual void M(A1 &) = 0;
virtual void M(A2 &) = 0;
};
class A1 : public A {
public:
virtual void M(A &a) { a.M(*this); }
virtual void M(A1 &a) { std::cout << "A1 <- A1\n"; }
virtual void M(A2 &a) { std::cout << "A2 <- A1\n"; }
};
class A2 : public A {
public:
virtual void M(A &a) { a.M(*this); }
virtual void M(A1 &a) { std::cout << "A1 <- A2\n"; }
virtual void M(A2 &a) { std::cout << "A2 <- A2\n"; }
};
(See e.g. http://ideone.com/nycls.)
I don't think there is a way of avoiding the multiple overloads in the base class.
This is double dispatch. When you write:
A* p1;
A* p2;
p1->M(*p2);
should dispatch both on the type of *p1 and the type of *p2.
Before starting, you must realize that this means n^2 functions for
n different derived types. And that somewhere, someone must be aware
of all of the derived types (unless you can define some sort of
"default" implemenation for an unknown pair of types).
There are two ways of implementing this. The simplest, if the hierarchy
is closed (i.e. client code cannot introduce new derived classes) does
use a host of virtual functions in the base class—generally
protected, since they're not designed to be called outside the
hierarchy:
// Forward references needed for all derived classes...
class A1;
class A2;
// ...
class A
{
protectd:
virtual void doM(A1* arg) = 0;
virtual void doM(A2* arg) = 0;
// ...
public:
virtual void M(A& arg) = 0;
};
In the derived classes, the implementation of M is always the same:
void A1::M(A& arg)
{
arg.doM( this );
}
This is simple, and relatively efficient, but requires changes in the
abstract base and all of the derived classes (which have to implement
the new virtual function) each time you add a new derived class. It's
useful for closed hierarchies, however; I've used it in classes using
the strategy pattern for part of their behavior, where the various
strategies were all defined in the source file, and not exposed to the
clients (and the abstract base of the strategies was only forward
declared in the header, so no header changes were necessary if I added a
strategy).
A more general solution would involve an std::map, with a pair of
typeid as index. You can't use typeid directly, since it's not
copyable. C++11 provides a type_index to wrap it; if you're using an
older compiler, it's fairly trivial to implement one yourself. The
basic principle is something along the lines of (probably in A itself):
typedef std::pair<std::type_index, std::type_index> TypePairKey;
typedef void (*FuncPtr)( M* arg1, M* arg2 );
typedef std::unordered_map<TypePairKey, FuncPtr> DispatchMap;
static DispatchMap ourDispatchMap;
with:
void M( A& arg ) // NOT virtual !!!
{
DispatchMap::iterator entry
= ourDispatchMap.find(
DispatchMap::value_type( typeid( *this ), typeid( arg ) ) );
assert( entry != ourDispatchMap.end() );
// Or some default handling, maybe throw NotYetImplemented()
(*entry->second)( this, &arg );
}
The real problem comes with writing each of the individual functions and
inserting their addresses in the map (before first use). The functions
themselves, of course, can use dynamic_cast, or even static_cast, if
you can be sure that they will only be called from here, and they can be
friend(s) of the class(es) involved, but there are still
n2 of them. (One frequent solution is to make them
static members of one of the classes, and to have each derived class
define a static member of a type which does the registration of all of
the functions its responsible for.)
Why not do something like that?
void A1::M( A a )
{
if( dynamic_cast< A1* >( &a ) )
{
// do your A1::M1(A1 a) stuff
}
else
if( dynamic_cast< A2* >( &a ) )
{
// do your A1::M2(A2 a) stuff
}
else
{
throw std::logic_error( "Unsupported A type." );
}
}
And do equivalently for A2::M?
If you want polymorphic behavior - then all you need is one method in Base class A. You can then reimplement that method in A1, A2.
After that you can write:
A *a1 = new A1();
A *a2 = new A2();
a1->M(a2); //polymorphic behavior
If you make something like this:
struct A
{
virtual void M(A *a) {}
};
struct A1 : public A
{
virtual void M(A1 *a) {cout << "A1" << endl;}
virtual void M(A *a) {cout << "A" << endl;}
};
Then:
A1 * a1 = new A1();
a1->M(a1); //prints "A1"
A * a = a1;
a->M(a1); //prints "A"
I don't think this the behavior you want

Mutual return types of member functions (C++)

Is it possible in C++ to have two classes, let's call them A and B, such that A has a member function f that returns an object of class B, and B has a member function g that returns an object of class A?
(The text below is just to show I have "done my homework".)
The problem is just how to write signatures of these functions, when the one in first defined class will have an incomplete return type. Forward declarations don't help here, because objects are returned by value.
Yes, I know all the workarounds (friend global functions, returning by pointer,...), but I would just like to know if the interface as above can be implemented in C++. For the sake of an example, let's say that I am trying to overload operator() on class A to return B, and on class B to return A. Since I am overloading operators, I must return by value (well, unless I want a dynamic allocation hell:), and () must be overloaded as a member function, so I can't use global friends.
Yes, Implement function definitions of class A after you declared class B
class B;
class A
{
B f();
};
class B
{
A g() { A a; return a; }
};
B A::f(){ B b; return b; }
Another possible way to break the dependency loop is to use a template member function, like this:
struct A {
template<typename T> T f() { return T(); }
};
struct B {
A g() { return A(); }
};
int main() {
A a;
B b = a.f<B>();
A a1 = b.g();
return 0;
}
It will work for operator() too, though the call syntax will be rather ugly: a.operator()<B>()