Can I Perform inheritance outside of the class in C++11? - c++

Say I want to overload a function outside of my classes with different pointer types. Can I do this in C++11?
struct Bird;
struct Bear;
struct Animal {
virtual Bird* AsBird() = 0;
virtual Bear* AsBear() = 0;
};
struct Bird : public Animal{
virtual Bird* AsBird(){ return this; }
virtual Bear* AsBear(){ return NULL; }
};
struct Bear : public Animal{
virtual Bird* AsBird(){ return NULL; }
virtual Bear* AsBear(){ return this; }
};
void Print(Animal* a){
cout << "I don't know what animal this is!" << endl;
}
void Print(Bear* b){
cout << "That's a bear!" << endl;
}
void Print(Bird* b){
cout << "That's a bird!" << endl;
}
int main(int argc, char* argv[]){
Animal* a = new Bear;
Bear* bear;
Bird* bird;
if (bear = a->AsBear()){
Print(bear);
} else if (bird = a->AsBird()){
Print(bird);
}
return 0;
}
This code works, but it's absolutely awful. I have tried playing around with templates and auto, but the compiler doesn't want anything to do with my evil experiments. Is there a legitimate way of doing this?

What you did is overloading the Print free function by changing the type of its parameter, there is no inheritance involved, and this is perfectly legal.
But you don't need it (or any dynamic_cast-like as you did) : What you should do is add a virtual void Print() const = 0 in your Animal base class instead, and override it in each derived class.
Example:
struct Animal {
virtual void Print() const = 0;
};
struct Bird : public Animal{
void Print() const { cout << "That's a bird!\n"; }
};
struct Bear : public Animal{
void Print() const { cout << "That's a bear!\n"; }
};
int main(){
Animal* a = new Bear;
a->Print();
Animal* b = new Bird;
b->Print();
}

As quantdev noted, the traditional way is adding a virtual function to your class hierarchy.
However, if you don't want to do it, you can use dynamic_cast, which was invented specifically for purposes like this.
struct Animal {
virtual ~Animal() {} // base class must have a virtual method to use dynamic_cast
};
...
if (bear = dynamic_cast<Bear*>(a)){
Print(bear);
} else if (bird = dynamic_cast<Bird*>(a)){
Print(bird);
}
This is a little better than what you have: if you add another inheriting class, you don't have to change your base class; you must only change your main function and add a new printing function.
If this is still "awful", maybe you should make Print a virtual function.

This looks like a use case for the visitor pattern.
struct Bird;
struct Bear;
struct Visitor
{
virtual void Visit(Bird& x) = 0;
virtual void Visit(Bear& x) = 0;
};
struct PrintVisitor : Visitor
{
void Visit(Bird& x) override { cout << "That's a bird!" << endl; };
void Visit(Bear& x) override { cout << "That's a bear!" << endl; };
};
struct Animal
{
virtual void Accept(Visitor& v) = 0;
};
struct Bird : public Animal
{
void Accept(Visitor& v) override { v.Visit(*this); }
};
struct Bear : public Animal
{
void Accept(Visitor& v) override { v.Visit(*this); }
};
int main(int argc, char* argv[])
{
Bear bear;
Bird bird;
PrintVisitor visitor;
Animal* a = &bear;
a->Accept(visitor);
a = &bird;
a->Accept(visitor);
}
Of course, it might just be easier to make Print a virtual member function.

The AsBear, AsBird functions, you've simply reimplemented dynamic_cast.
As far as the overloads of Print goes, the simplest solution here is to make Print be a virtual function in the Animal hierarchy.
My recommended solution would be to use the visitor pattern. Since that's what it seems like you're trying to do by decoupling the Print function and the class hierarchy.
#include <iostream>
struct Bird;
struct Bear;
struct Animal {
struct Visitor {
virtual void operator()(const Bird *) const = 0;
virtual void operator()(const Bear *) const = 0;
};
virtual void Accept(const Visitor &visitor) const = 0;
};
struct Bird : public Animal {
virtual void Accept(const Visitor &visitor) const override {
visitor(this);
}
};
struct Bear : public Animal {
virtual void Accept(const Visitor &visitor) const override {
visitor(this);
}
};
struct Print : public Animal::Visitor {
virtual void operator()(const Bird *) const override {
std::cout << "Bird" << std::endl;
}
virtual void operator()(const Bear *) const override {
std::cout << "Bear" << std::endl;
}
};
int main() {
Bird bird;
Animal *animal = &bird;
animal->Accept(Print());
}

Related

Virtual template function in c++

I have been looking for a way to use both templating and polymorphism at the same time. Here's a simplified version of my problem:
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
//*******************************************************************
//*******************************************************************
struct DerivedStuff1
{
static void eval() { cout << "evaluating DerivedStuff1" << endl; }
};
struct DerivedStuff2
{
static void eval() { cout << "evaluating DerivedStuff2" << endl; }
};
//*******************************************************************
//*******************************************************************
class BaseClass
{
public:
template<typename StuffType> virtual void eval() const = 0;
};
class DerivedClass1 : public BaseClass
{
public:
template<typename StuffType> virtual void eval() const
{
std::cout << "We are in DerivedClass1: ";
StuffType::eval();
}
};
class DerivedClass2 : public BaseClass
{
public:
template<typename StuffType> virtual void eval() const
{
std::cout << "We are in DerivedClass2: ";
StuffType::eval();
}
};
int main()
{
BaseClass* c1 = new DerivedClass1;
c1->eval<DerivedStuff1>();
c1->eval<DerivedStuff2>();
BaseClass* c2 = new DerivedClass2;
c2->eval<DerivedStuff1>();
c2->eval<DerivedStuff2>();
return 0;
}
This code does not compile because virtual template functions are not allowed in C++. I found a few approaches to tackle this problem (CRTP, etc.) but none of them were really satisfying. Is there no elegant way to get around that issue?
The visitor pattern turns run-time polymorphism on its side and makes runtime-polymorphic function templates possible. It has other legitimate uses apart from templatisation, so I guess you can call it somewhat elegant.
Your example can look as follows:
#include <iostream>
class DerivedStuff1 {
public:
static void eval() { std::cout << "Evaluating DerivedStuff1\n"; }
};
class DerivedStuff2 {
public:
static void eval() { std::cout << "Evaluating DerivedStuff2\n"; }
};
class DerivedClass1; class DerivedClass2;
class BaseClassVisitor {
public:
virtual void visit(DerivedClass1&) = 0;
virtual void visit(DerivedClass2&) = 0;
};
class BaseClass {
public:
virtual void accept(BaseClassVisitor& v) = 0;
};
class DerivedClass1 : public BaseClass
{
public:
virtual void accept(BaseClassVisitor& v) { v.visit(*this); }
};
class DerivedClass2 : public BaseClass
{
public:
virtual void accept(BaseClassVisitor& v) { v.visit(*this); }
};
template <typename StuffType>
class EvalVisitor : public BaseClassVisitor
{
virtual void visit(DerivedClass1&) {
std::cout << "We are in DerivedClass1: ";
StuffType::eval();
}
virtual void visit(DerivedClass2&) {
std::cout << "We are in DerivedClass2: ";
StuffType::eval();
}
};
int main()
{
EvalVisitor<DerivedStuff1> e1;
EvalVisitor<DerivedStuff2> e2;
BaseClass* c1 = new DerivedClass1;
c1->accept(e1);
c1->accept(e2);
BaseClass* c2 = new DerivedClass2;
c2->accept(e1);
c2->accept(e2);
return 0;
}
Demo
Of course all shortcomings of Visitor apply here.
You could reinvent the vtable and resolve the function pointer at run time. You will, however, have to explicitely instantiate the template on the derived class, but I don't see any approach to this that won't require that.
Quick and dirty example:
#include <map>
#include <iostream>
class Base {
public:
typedef void (Base::*eval_ptr)();
using eval_vtable = std::map<std::type_index, eval_ptr>;
Base(eval_vtable const& eval_p) : eval_ptrs(eval_p) {}
template<typename T>
void eval() {
auto handler = eval_ptrs.find(type_index(typeid(T)));
if(handler != eval_ptrs.end()) {
auto handler_ptr = handler->second;
(this->*handler_ptr)();
}
}
eval_vtable const& eval_ptrs;
};
class Derived : public Base {
public:
Derived()
: Base(eval_functions) {}
template<typename T>
void eval_impl() {
std::cout << typeid(T).name() << "\n";
}
static eval_vtable eval_functions;
};
Base::eval_vtable Derived::eval_functions = {
{ type_index(typeid(int)), eval_ptr(&Derived::eval_impl<int>) },
{ type_index(typeid(float)), eval_ptr(&Derived::eval_impl<float>) },
{ type_index(typeid(short)), eval_ptr(&Derived::eval_impl<short>) },
};
int main(int argc, const char* argv[]) {
Derived x;
Base * x_as_base = &x;
x_as_base->eval<int>(); // calls Derived::eval_impl<int>()
return 0;
}
This won't be exactly fast, but it will give you the closest thing to templated virtual functions that I can think of.
Edit: For the record I don't advocate anyone use this. I would much rather revisit the design to avoid being painted in this particular corner in the first place. Please consider my answer as an academic solution to a theoretical problem, not an actual engineering recommendation.
Since virtual template methods in C++ arent allowed, you can make a class template and call static function of class template param.
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
//*******************************************************************
//*******************************************************************
struct DerivedStuff1
{
static void eval() { cout << "evaluating DerivedStuff1" << endl; }
};
struct DerivedStuff2
{
static void eval() { cout << "evaluating DerivedStuff2" << endl; }
};
//*******************************************************************
//*******************************************************************
class BaseClass
{
public:
virtual void eval() const = 0;
};
template<typename StuffType>
class DerivedClass1 : public BaseClass
{
public:
virtual void eval() const
{
std::cout << "We are in DerivedClass1: ";
StuffType::eval();
}
};
template<typename StuffType>
class DerivedClass2 : public BaseClass
{
public:
virtual void eval() const
{
std::cout << "We are in DerivedClass2: ";
StuffType::eval();
}
};
int main()
{
BaseClass* c1 = new DerivedClass1<DerivedStuff1>;
c1->eval();
c1 = new DerivedClass1<DerivedStuff2>;
c1->eval();
BaseClass* c2 = new DerivedClass2<DerivedStuff1>;
c2->eval();
c2 = new DerivedClass2<DerivedStuff2>;
c2->eval();
// deletes
return 0;
}
Output
We are in DerivedClass1: evaluating DerivedStuff1
We are in DerivedClass1: evaluating DerivedStuff2
We are in DerivedClass2: evaluating DerivedStuff1
We are in DerivedClass2: evaluating DerivedStuff2
You cannot mix templates (compile time) and polymorphic (runtime). That's it.
So, a posible workaround is remove templates. For example, it could take a function pointer or just more polymorphism:
//*******************************************************************
//*******************************************************************
struct InterfaceStuff{
virtual void eval() = 0;
}
struct DerivedStuff1 : public InterfaceStuff
{
void eval() { cout << "evaluating DerivedStuff1" << endl; }
};
struct DerivedStuff2 : public InterfaceStuff
{
void eval() { cout << "evaluating DerivedStuff2" << endl; }
};
//*******************************************************************
//*******************************************************************
class BaseClass
{
public:
virtual void eval(InterfaceStuff* interface) const = 0;
};
class DerivedClass1 : public BaseClass
{
public:
virtual void eval(InterfaceStuff* interface) const
{
std::cout << "We are in DerivedClass1: ";
interface->eval();
}
};
class DerivedClass2 : public BaseClass
{
public:
virtual void eval(InterfaceStuff* interface) const
{
std::cout << "We are in DerivedClass2: ";
interface->eval();
}
};
Another posible workaround is remove polymorphism, just use more templates:
struct DerivedStuff1
{
static void eval() { cout << "evaluating DerivedStuff1" << endl; }
};
struct DerivedStuff2
{
static void eval() { cout << "evaluating DerivedStuff2" << endl; }
};
//*******************************************************************
//*******************************************************************
class BaseClass
{
public:
template<typename Eval,typename StuffType> void eval() const
{
Eval::eval();
StuffType::eval();
}
};
class DerivedClass1 : public BaseClass
{
};
class DerivedClass2 : public BaseClass
{
};
One way of another, you have to choose one.

C++ Interface With Explicit Derived Methods

I have a sample interface that delegates behavior to the implementing class:
class IBase
{
public:
virtual void Do(IBase* base) = 0;
virtual std::string name() = 0;
};
I then have 1..N classes that implement IBase:
class A : public IBase
{
public:
virtual void Do(IBase* base);
virtual std::string name() { return "A"; }
};
class B : public IBase
{
public:
virtual void Do(IBase* base);
virtual std::string name() { return "B"; }
};
I then want the body of the Do() method to call free methods that are defined for everything that implements IBase:
void method(A* a, B* b)
{
std::cout << a->name() << " " << b->name() << std::endl;
}
void method(B* b, A* a)
{
method(b, a);
}
This doesn't compile because with this code because IBase cannot resolve to the derived type:
void Test::Run()
{
IBase* a = new A();
IBase* b = new B();
b->Do(a);
}
How do I make this work, or something similar? The free methods implement all possible combinations, and it seems like there is a trick to get the IBase* to be acceptable in one of the overloads.
Secondly, how do you implement a interface scheme where each implementer has a shared method that takes an interface? Perhaps this is better implemented with just free methods and removing the Do(IBase*) from the IBase interface.
Edit:
It works if (a) is declared to be type A. Whats the best way to make the code above work with the IBase?
void Test::Run()
{
A* a = new A();
IBase* b = new B();
b->Do(a);
}
Literal code I am compiling:
class IBase
{
public:
virtual void Do(IBase* base) = 0;
virtual std::string name() = 0;
};
class A : public IBase
{
public:
virtual void Do(IBase* base);
virtual std::string name();
};
class B : public IBase
{
public:
virtual void Do(IBase* base);
virtual std::string name();
};
class Test
{
public:
static void Run();
};
namespace
{
void method(A* a, B* b)
{
std::cout << a->name() << " " << b->name() << std::endl;
}
void method(B* b, A* a)
{
method(b, a);
}
}
void A::Do(IBase* base)
{
method(this, base);
}
std::string A::name()
{
return "A";
}
void B::Do(IBase* base)
{
method(this, base);
}
std::string B::name()
{
return "B";
}
void Test::Run()
{
IBase* a = new A();
IBase* b = new B();
b->Do(a);
}
Visual Studio 2013:
Error 1 error C2665: 'anonymous-namespace'::method' : none of the 2 overloads could convert all the argument types
Error 2 error C2665: 'anonymous-namespace'::method' : none of the 2 overloads could convert all the argument types
So if I understand you correctly, you want A::Do to look like this:
void A::Do(IBase* other) {
if other is A, then call:
method(this,other) for arguments A,A
else if other is B, then call
method(this,other) for arguments A,B
etc.
}
There are two answers to that. The best approach is usually to change the design. Make method a virtual function in IBase instead of a free function, and extract any functionality specific to A and B into yet another virtual function.
class IBase
{
public:
virtual void Do(IBase* base) = 0;
virtual std::string name() = 0;
virtual void method(IBase* other);
virtual void method2() = 0;
};
void IBase::method(IBase* other) {
std::cout << name() << " " << other->method2() << std::endl;
}
The other option is to use type casting:
void A::Do(IBase* other) {
if other is A, then call:
method(this,dynamic_cast<A*>(other)))
else if other is B, then call
method(this,dynamic_cast<B*>(other))
etc.
}
This approach usually does not scale well, is hard to maintain and error-prone.

C++ virtual functions base return type suggestions

I need a base class that gives me primitive type of data's pointer. I add a function in it. I derived types of class. I used void * to support all primitive types as a return type but it is like old C days. It is not good for OOP. Does one have an suggestion to do in a proper way in OOP?
#include <iostream>
class base {
public:
virtual void *getPtr() = 0;
virtual ~base() {};
};
class derivedAType : public base {
protected:
int _i;
public:
derivedAType(int i): _i(0) { _i = i; };
virtual ~derivedAType() {}
virtual void *getPtr() {
return static_cast<void *>(&_i);
}
};
class derivedBType : public base {
protected:
short _s;
public:
derivedBType(short s): _s(0) { _s = s; };
virtual ~derivedBType() {}
virtual void *getPtr() {
return static_cast<void *>(&_s);
}
};
int main()
{
base *b1 = new derivedAType(1203912);
base *b2 = new derivedBType(25273);
std::cout << "b1 : " << *(static_cast<int *>(b1->getPtr()))
<< "\nb2 : " << *(static_cast<short *>(b2->getPtr()))
<< std::endl;
delete b2;
delete b1;
return 0;
}
Make the base class a template class with the data type as the template variable
template<typename DataType>
class base {
virtual DataType* getPtr() = 0;
//...
};
and
class derivedAType : public base<int>
But this changes base class to a template class which means you cant store them together, base<int> is different from base<short>
If this isnt acceptable, the other options is just a tad bit cleaner than your code but abt the same, refer to this question. Basically derived class return types can reflect their true type and i think it should get automatically converted to void*, so you dont have to manually cast the pointer.
Not sure about your problem. But maybe a double callback can help:
class Callback {
public:
virtual void do_int( int i ) const = 0;
virtual void do_short( short s ) const = 0;
/* ... */
}
class base {
public:
virtual void do_stuff(const Callback & c); /* will need a more telling name */
virtual ~base() {};
};
class derivedAType : public base {
protected:
int _i;
public:
derivedAType(int i): _i(0) { _i = i; };
virtual ~derivedAType() {}
virtual void do_stuff(const Callback & c) {
c.do_int( _i );
}
};
class derivedBType : public base {
protected:
short _s;
public:
derivedBType(short s): _s(0) { _s = s; };
virtual ~derivedBType() {}
virtual void do_stuff( const Callback & c) {
c.do_short( _s );
}
};
class print_callback : public Callback {
public:
virtual void do_int( int i ) const { std::cout << i; }
virtual void do_short( short s ) const { std::cout << s; }
}
int main() {
base *b1 = new derivedAType(1203912);
base *b2 = new derivedBType(25273);
std::cout << "b1 : ";
b1->do_stuff(print_callback());
std::cout << "\nb2 : ";
b2->do_stuff(print_callback());
std::cout << std::endl;
delete b2;
delete b1;
return 0;
}
Of course you can simplify this by just storing the created print callback, and using it twice.

How to achieve "virtual template function" in C++

first off: I have read and I know now that a virtual template member function is not (yet?) possible in C++. A workaround would be to make the class a template and then use the template-argument also in the member-function.
But in the context of OOP, I find that the below example would not be very "natural" if the class was actually a template. Please note that the code is actually not working, but the gcc-4.3.4 reports: error: templates may not be ‘virtual’
#include <iostream>
#include <vector>
class Animal {
public:
template< class AMOUNT >
virtual void eat( AMOUNT amount ) const {
std::cout << "I eat like a generic Animal." << std::endl;
}
virtual ~Animal() {
}
};
class Wolf : public Animal {
public:
template< class AMOUNT >
void eat( AMOUNT amount) const {
std::cout << "I eat like a wolf!" << std::endl;
}
virtual ~Wolf() {
}
};
class Fish : public Animal {
public:
template< class AMOUNT >
void eat( AMOUNT amount) const {
std::cout << "I eat like a fish!" << std::endl;
}
virtual ~Fish() {
}
};
class GoldFish : public Fish {
public:
template< class AMOUNT >
void eat( AMOUNT amount) const {
std::cout << "I eat like a goldfish!" << std::endl;
}
virtual ~GoldFish() {
}
};
class OtherAnimal : public Animal {
virtual ~OtherAnimal() {
}
};
int main() {
std::vector<Animal*> animals;
animals.push_back(new Animal());
animals.push_back(new Wolf());
animals.push_back(new Fish());
animals.push_back(new GoldFish());
animals.push_back(new OtherAnimal());
for (std::vector<Animal*>::const_iterator it = animals.begin(); it != animals.end(); ++it) {
(*it)->eat();
delete *it;
}
return 0;
}
So creating a "Fish< Amount > foo" is kind of strange. However, it seems desirable to me to provide an arbitrary amount of food to eat for each animal.
Thus, I am searching a solution about how to achieve something like
Fish bar;
bar.eat( SomeAmount food );
This becomes particularly useful when looking at the for-loop. One might like to feed a specific amount (FoodAmount) to all of the different animals (via eat() and bind1st() e.g.), it could not be done that easily, although I wound find this very inuitive (and thus to some extent "natural). While some might want to argue now that this is due to the "uniform"-character of a vector, I think/wish that it should be possible to achieve this and I really would like to know how, as this is puzzling me for quite some time now...
[EDIT]
To perhaps clarify the motivation behind my question, I want to program an Exporter-class and let different, more specialized classes derive from it. While the top-level Exporter-class is generally only for cosmetic/structural purpose, a GraphExporter-class is derived, that should again serve as a base-class for even more specialzed export. However, similar to the Animal-example, I would like to be able to define GraphExporter* even on specialized/derived classes (e.g. on SpecialGraphExplorer) but when calling "write( out_file )", it should call the appropriate member function for SpecialGraphExporter instead of GraphExporter::write( out_file).
Maybe this makes my situation and intentions clearer.
Best,
Shadow
After some thinking I recognized this as the classic multi-method requirement, i.e. a method that dispatches based on the runtime type of more than one parameter. Usual virtual functions are single dispatch in comparison (and they dispatch on the type of this only).
Refer to the following:
Andrei Alexandrescu has written (the seminal bits for C++?) on implementing multi-methods using generics in 'Modern C++ design'
Chapter 11: "Multimethods" - it implements basic multi-methods, making them logarithmic (using ordered typelists) and then going all the way to constant-time multi-methods. Quite powerful stuff !
A codeproject article that seems to have just such an implementation:
no use of type casts of any kind (dynamic, static, reinterpret, const or C-style)
no use of RTTI;
no use of preprocessor;
strong type safety;
separate compilation;
constant time of multimethod execution;
no dynamic memory allocation (via new or malloc) during multimethod call;
no use of nonstandard libraries;
only standard C++ features is used.
C++ Open Method Compiler, Peter Pirkelbauer, Yuriy Solodkyy, and Bjarne Stroustrup
The Loki Library has A MultipleDispatcher
Wikipedia has quite a nice simple write-up with examples on Multiple Dispatch in C++.
Here is the 'simple' approach from the wikipedia article for reference (the less simple approach scales better for larger number of derived types):
// Example using run time type comparison via dynamic_cast
struct Thing {
virtual void collideWith(Thing& other) = 0;
}
struct Asteroid : Thing {
void collideWith(Thing& other) {
// dynamic_cast to a pointer type returns NULL if the cast fails
// (dynamic_cast to a reference type would throw an exception on failure)
if (Asteroid* asteroid = dynamic_cast<Asteroid*>(&other)) {
// handle Asteroid-Asteroid collision
} else if (Spaceship* spaceship = dynamic_cast<Spaceship*>(&other)) {
// handle Asteroid-Spaceship collision
} else {
// default collision handling here
}
}
}
struct Spaceship : Thing {
void collideWith(Thing& other) {
if (Asteroid* asteroid = dynamic_cast<Asteroid*>(&other)) {
// handle Spaceship-Asteroid collision
} else if (Spaceship* spaceship = dynamic_cast<Spaceship*>(&other)) {
// handle Spaceship-Spaceship collision
} else {
// default collision handling here
}
}
}
Obviously, virtual member function templates are not allowed and could not be realized even theoretically. To build a base class' virtual table, there needs to be a finite number of virtual function-pointer entries. A function template would admit an indefinite amount of "overloads" (i.e. instantiations).
Theoretically-speaking, a language (like C++) could allow virtual member function templates if it had some mechanism to specify the actual (finite) list of instantiations. C++ does have that mechanism (i.e. explicit template instantiations), so I guess it could be possible to do this in a newer C++ standard (although I have no idea what trouble it would entail for compiler vendors to implement this feature). But, that's just a theoretical discussion, in practice, this is simply not allowed. The fact remains, you have to make the number of virtual functions finite (no templates allowed).
Of course, that doesn't mean that class template cannot have virtual functions, nor does it mean that virtual functions cannot call function templates. So, there are many solutions in that vein (like the Visitor pattern or other schemes).
One solution that, I think, serves your purpose (although it is hard to comprehend) elegantly is the following (which is basically a visitor pattern):
#include <iostream>
#include <vector>
struct Eater {
virtual void operator()(int amount) const = 0;
virtual void operator()(double amount) const = 0;
};
template <typename EaterType>
struct Eater_impl : Eater {
EaterType& data;
Eater_impl(EaterType& aData) : data(aData) { };
virtual void operator()(int amount) const { data.eat_impl(amount); };
virtual void operator()(double amount) const { data.eat_impl(amount); };
};
class Animal {
protected:
Animal(Eater& aEat) : eat(aEat) { };
public:
Eater& eat;
virtual ~Animal() { delete &eat; };
};
class Wolf : public Animal {
private:
template< class AMOUNT >
void eat_impl( AMOUNT amount) const {
std::cout << "I eat like a wolf!" << std::endl;
}
public:
friend struct Eater_impl<Wolf>;
Wolf() : Animal(*(new Eater_impl<Wolf>(*this))) { };
virtual ~Wolf() { };
};
class Fish : public Animal {
private:
template< class AMOUNT >
void eat_impl( AMOUNT amount) const {
std::cout << "I eat like a fish!" << std::endl;
}
public:
friend struct Eater_impl<Fish>;
Fish() : Animal(*(new Eater_impl<Fish>(*this))) { };
virtual ~Fish() { };
};
int main() {
std::vector<Animal*> animals;
animals.push_back(new Wolf());
animals.push_back(new Fish());
for (std::vector<Animal*>::const_iterator it = animals.begin(); it != animals.end(); ++it) {
(*it)->eat(int(0));
(*it)->eat(double(0.0));
delete *it;
};
return 0;
};
The above is a neat solution because it allows you to define a finite number of overloads that you want in one place only (in the Eater_impl class template) and all you need in the derived class is a function template (and possibly additional overloads, for special cases). There is, of course, a bit of overhead, but I guess that a bit more thought could be put into it to reduce the overhead (additional reference storage and dynamic allocation of Eater_impl). I guess, the curiously recurring template pattern could probably be employed somehow to this end.
I think the visitor pattern can be a solution.
UPDATE
I finished my example:
#include <iostream>
#include <vector>
#include <boost/shared_ptr.hpp>
class Animal;
class Wolf;
class Fish;
class Visitor
{
public:
virtual void visit(const Animal& p_animal) const = 0;
virtual void visit(const Wolf& p_animal) const = 0;
virtual void visit(const Fish& p_animal) const = 0;
};
template<class AMOUNT>
class AmountVisitor : public Visitor
{
public:
AmountVisitor(AMOUNT p_amount) : m_amount(p_amount) {}
virtual void visit(const Animal& p_animal) const
{
std::cout << "I eat like a generic Animal." << std::endl;
}
virtual void visit(const Wolf& p_animal) const
{
std::cout << "I eat like a wolf!" << std::endl;
}
virtual void visit(const Fish& p_animal) const
{
std::cout << "I eat like a fish!" << std::endl;
}
AMOUNT m_amount;
};
class Animal {
public:
virtual void Accept(const Visitor& p_visitor) const
{
p_visitor.visit(*this);
}
virtual ~Animal() {
}
};
class Wolf : public Animal {
public:
virtual void Accept(const Visitor& p_visitor) const
{
p_visitor.visit(*this);
}
};
class Fish : public Animal {
public:
virtual void Accept(const Visitor& p_visitor) const
{
p_visitor.visit(*this);
}
};
int main()
{
typedef boost::shared_ptr<Animal> TAnimal;
std::vector<TAnimal> animals;
animals.push_back(TAnimal(new Animal()));
animals.push_back(TAnimal(new Wolf()));
animals.push_back(TAnimal(new Fish()));
AmountVisitor<int> amount(10);
for (std::vector<TAnimal>::const_iterator it = animals.begin(); it != animals.end(); ++it) {
(*it)->Accept(amount);
}
return 0;
}
this prints:
I eat like a generic Animal.
I eat like a wolf!
I eat like a fish!
Per Mikael's post, I have made another offshoot, using the CRTP and following Eigen's style of using derived() for an explicit subclass reference:
// Adaptation of Visitor Pattern / CRTP from:
// http://stackoverflow.com/a/5872633/170413
#include <iostream>
using std::cout;
using std::endl;
class Base {
public:
virtual void tpl(int x) = 0;
virtual void tpl(double x) = 0;
};
// Generics for display
template<typename T>
struct trait {
static inline const char* name() { return "T"; }
};
template<>
struct trait<int> {
static inline const char* name() { return "int"; }
};
template<>
struct trait<double> {
static inline const char* name() { return "double"; }
};
// Use CRTP for dispatch
// Also specify base type to allow for multiple generations
template<typename BaseType, typename DerivedType>
class BaseImpl : public BaseType {
public:
void tpl(int x) override {
derived()->tpl_impl(x);
}
void tpl(double x) override {
derived()->tpl_impl(x);
}
private:
// Eigen-style
inline DerivedType* derived() {
return static_cast<DerivedType*>(this);
}
inline const DerivedType* derived() const {
return static_cast<const DerivedType*>(this);
}
};
// Have Child extend indirectly from Base
class Child : public BaseImpl<Base, Child> {
protected:
friend class BaseImpl<Base, Child>;
template<typename T>
void tpl_impl(T x) {
cout << "Child::tpl_impl<" << trait<T>::name() << ">(" << x << ")" << endl;
}
};
// Have SubChild extend indirectly from Child
class SubChild : public BaseImpl<Child, SubChild> {
protected:
friend class BaseImpl<Child, SubChild>;
template<typename T>
void tpl_impl(T x) {
cout << "SubChild::tpl_impl<" << trait<T>::name() << ">(" << x << ")" << endl;
}
};
template<typename BaseType>
void example(BaseType *p) {
p->tpl(2);
p->tpl(3.0);
}
int main() {
Child c;
SubChild sc;
// Polymorphism works for Base as base type
example<Base>(&c);
example<Base>(&sc);
// Polymorphism works for Child as base type
example<Child>(&sc);
return 0;
}
Output:
Child::tpl_impl<int>(2)
Child::tpl_impl<double>(3)
SubChild::tpl_impl<int>(2)
SubChild::tpl_impl<double>(3)
SubChild::tpl_impl<int>(2)
SubChild::tpl_impl<double>(3)
This snippet may be found in source here: repro:c808ef0:cpp_quick/virtual_template.cc
Virtual template function is not allowed. However you can use one OR the other here.
You could make an interface using virtual methods and implement your various animals in terms of having an eating interface. (i.e. PIMPL)
Less human intuitive would be having a non-member non-friend template function as a free function which could take templated const reference to any animal and make them eat accordingly.
For the record you don't need templates here. Pure virtual abstract method on the base class is enough to force and interface where all animals must eat and define how they do so with an override, providing a regular virtual would be enough to say all animals can eat but if they don't have a specific way then they can use this default way.
You can create a template class with virtual function, and implement the function in the derived class without using template in the follwing way:
a.h:
template <class T>
class A
{
public:
A() { qDebug() << "a"; }
virtual A* Func(T _template) { return new A;}
};
b.h:
class B : public A<int>
{
public:
B();
virtual A* Func(int _template) { return new B;}
};
and the function CTOR and call
A<int>* a1=new B;
int x=1;
a1->Func(x);
unfortunately i havn't found a way to create a virtual function with template parameters without declaring the class as a template and it template type on the dervied class
I have copied your code and modified it, so now it should work exactly as you want:
#include <iostream>
#include <vector>
//defined new enum type
enum AnimalEnum
{
animal,
wolf,
fish,
goldfish,
other
};
//forward declarations
class Wolf;
class Fish;
class GoldFish;
class OtherAnimal;
class Animal {
private:
AnimalEnum who_really_am_I;
void* animal_ptr;
public:
//declared new constructors overloads for each type of animal
Animal(const Animal&);
Animal(const Wolf&);
Animal(const Fish&);
Animal(const GoldFish&);
Animal(const OtherAnimal&);
template< class AMOUNT >
/*removed the virtual keyword*/ void eat( AMOUNT amount ) const {
switch (this->who_really_am_I)
{
case AnimalEnum::other: //You defined OtherAnimal so that it doesn't override the eat action, so it will uses it's Animal's eat
case AnimalEnum::animal: std::cout << "I eat like a generic Animal." << std::endl; break;
case AnimalEnum::wolf: ((Wolf*)this->animal_ptr)->eat(amount); break;
case AnimalEnum::fish: ((Fish*)this->animal_ptr)->eat(amount); break;
case AnimalEnum::goldfish: ((GoldFish*)this->animal_ptr)->eat(amount) break;
}
}
void DeleteMemory() { delete this->animal_ptr; }
virtual ~Animal() {
//there you can choose if whether or not to delete "animal_ptr" here if you want or not
}
};
class Wolf : public Animal {
public:
template< class AMOUNT >
void eat( AMOUNT amount) const {
std::cout << "I eat like a wolf!" << std::endl;
}
virtual ~Wolf() {
}
};
class Fish : public Animal {
public:
template< class AMOUNT >
void eat( AMOUNT amount) const {
std::cout << "I eat like a fish!" << std::endl;
}
virtual ~Fish() {
}
};
class GoldFish : public Fish {
public:
template< class AMOUNT >
void eat( AMOUNT amount) const {
std::cout << "I eat like a goldfish!" << std::endl;
}
virtual ~GoldFish() {
}
};
class OtherAnimal : public Animal {
//OtherAnimal constructors must be defined here as Animal's constructors
OtherAnimal(const Animal& a) : Animal(a) {}
OtherAnimal(const Wolf& w) : Animal(w) {}
OtherAnimal(const Fish& f) : Animal(f) {}
OtherAnimal(const GoldFish& g) : Animal(g) {}
OtherAnimal(const OtherAnimal& o) : Animal(o) {}
virtual ~OtherAnimal() {
}
};
//OtherAnimal will be useful only if it has it's own actions and members, because if not, typedef Animal OtherAnimal or using OtherAnimal = Animal can be used, and it can be removed from above declarations and below definitions
//Here are the definitions of Animal constructors that were declared above/before:
Animal::Animal(const Animal& a) : who_really_am_I(AnimalEnum::animal), animal_ptr(nullptr) {}
Animal::Animal(const Wolf& w) : who_really_am_I(AnimalEnum::wolf), animal_ptr(new Wolf(w)) {}
Animal::Animal(const Fish& f) : who_really_am_I(AnimalEnum::fish), animal_ptr(new Fish(f)) {}
Animal::Animal(const GoldFish& g) : who_really_am_I(AnimalEnum::goldfish), animal_ptr(new GoldFish(g)) {}
Animal::Animal(const OtherAnimal& o) :
who_really_am_I(AnimalEnum::other), animal_ptr(new OtherAnimal(o)) {}
int main() {
std::vector<Animal> animals;
animals.push_back(Animal());
animals.push_back(Wolf()); //Wolf is converted to Animal via constructor
animals.push_back(Fish()); //Fish is converted to Animal via constructor
animals.push_back(GoldFish()); //GoldFish is converted to Animal via constructor
animals.push_back(OtherAnimal()); //OtherAnimal is converted to Animal via constructor
for (std::vector<Animal>::const_iterator it = animals.begin(); it != animals.end(); ++it) {
it->eat(); //this is Animal's eat that invokes other animals eat
//delete *it; Now it should be:
it->DeleteMemory();
}
animals.clear(); //All animals have been killed, and we don't want full vector of dead animals.
return 0;
}
In you scenario, you are trying to mix compile time polymorphism with runtime polymorphism, but it cannot be done in this "direction".
Essential, your AMOUNT template argument represents an expected interface for the type to implement based on the union of all the operations each implementation of eat uses. If you where to create an abstract type that declared each of those operations making them virtual where needed, then you could call eat with different types (that derived from your AMOUNT interface). And it would behave as expected.
I don't work with templates, but I think:
(1) You cannot use templates inside a class, templates are more like global types or global variables.
(2) In O.O.P., the same problem you present, and that you are trying to solve by using templates, can be solved by using inheritance.
Classes work similar to templates, you can extended by adding new things, or replace things of classes with pointers, pointers to objects (A.K.A. "references") and overriding virtual functions.
#include <iostream>
struct Animal {
virtual void eat(int amount ) {
std::cout << "I eat like a generic Animal." << std::endl;
}
virtual ~Animal() { }
};
#if 0
// example 1
struct Wolf : Animal {
virtual void eat(int amount) {
std::cout << "I eat like a wolf!" << std::endl;
}
};
struct Fish : Animal {
virtual void eat(int amount) {
std::cout << "I eat like a fish!" << std::endl;
}
};
#else
// example 2
struct AnimalFood {
virtual int readAmount() { return 5; }
virtual void showName() {
std::cout << "I'm generic animal food" << std::endl;
}
};
struct PredatorFood : AnimalFood {
virtual int readAmount() { return 500; }
virtual void showName() {
std::cout << "I'm food for a predator" << std::endl;
}
};
struct Fish : Animal {
virtual void eat(AnimalFood* aFood) {
if (aFood->readAmount() < 50) {
std::cout << "OK food, vitamines: " << aFood->readAmount() << std::endl;
} else {
std::cout << "too much food, vitamines: " << aFood->readAmount() << std::endl;
}
}
};
struct Shark : Fish {
virtual void eat(AnimalFood* aFood) {
if (aFood->readAmount() < 250) {
std::cout << "too litle food for a shark, Im very hungry, vitamines: " << aFood->readAmount() << std::endl;
} else {
std::cout << "OK, vitamines: " << aFood->readAmount() << std::endl;
}
}
};
struct Wolf : Fish {
virtual void eat(AnimalFood* aFood) {
if (aFood->readAmount() < 150) {
std::cout << "too litle food for a wolf, Im very hungry, vitamines: " << aFood->readAmount() << std::endl;
} else {
std::cout << "OK, vitamines: " << aFood->readAmount() << std::endl;
}
}
};
#endif
int main() {
// find animals
Wolf* loneWolf = new Wolf();
Fish* goldenFish = new Fish();
Shark* sharky = new Shark();
// prepare food
AnimalFood* genericFood = new AnimalFood();
PredatorFood* bigAnimalFood = new PredatorFood();
// give food to animals
loneWolf->eat(genericFood);
loneWolf->eat(bigAnimalFood);
goldenFish->eat(genericFood);
goldenFish->eat(bigAnimalFood);
sharky->eat(genericFood);
sharky->eat(bigAnimalFood);
delete bigAnimalFood;
delete genericFood;
delete sharky;
delete goldenFish;
delete loneWolf;
}
Cheers.

Abstract object cannot declare

I'm having a problem with abstract/virtual classes, a replication of the problem here:
#include <iostream>
class A
{
protected:
virtual std::string getDateTime() = 0;
virtual void Write(std::string data, bool addDate) = 0;
virtual bool CheckFile() = 0;
virtual bool OpenFile(std::string path) = 0;
virtual void CloseFile() = 0;
};
class B
: public A
{
public:
virtual std::string ToString() { return ""; };
virtual void Write(std::string data) { };
};
class C
: public A
{
protected:
std::string getDateTime()
{
return "TODAY";
};
void Write(std::string data, bool addDate)
{
std::cout << "BasicClassA Write" << std::endl;
};
bool CheckFile()
{
std::cout << "BasicClassA CheckFile" << std::endl;
return true;
};
bool OpenFile(std::string path)
{
std::cout << "BasicClassA OpenFile" << std::endl;
return true;
};
void CloseFile()
{
std::cout << "BasicClassA CloseFile" << std::endl;
};
};
class D
: public B,
public C
{
public:
BasicClassB();
virtual ~BasicClassB();
std::string ToString()
{
return "BasicClassB tostring";
};
void Write(std::string data)
{
std::cout << "BasicClassB Write" << std::endl;
};
};
int main(int ac, char *av[])
{
BasicClassB b;
std::cout << b.ToString() << std::endl;
b.Write("");
return 0;
}
This has a compile error:
../src/main.cpp: In function ‘int main(int, char**)’:
../src/main.cpp:82: error: cannot declare variable ‘b’ to be of abstract type ‘BasicClassB’
../src/main.cpp:64: note: because the following virtual functions are pure within ‘BasicClassB’:
../src/main.cpp:13: note: virtual std::string BaseClassA::getDateTime()
../src/main.cpp:14: note: virtual void BaseClassA::Write(std::string, bool)
../src/main.cpp:15: note: virtual bool BaseClassA::CheckFile()
../src/main.cpp:16: note: virtual bool BaseClassA::OpenFile(std::string)
../src/main.cpp:17: note: virtual void BaseClassA::CloseFile()
Perhaps I'm missing the point here, but the implementation of BaseClassA (being BasicClassA) should contain these functions, and since BasicClassB is subclassed from BasicClassA as well, it should also contain these functions?
What am I missing? What should I do to make this compile?
[edit]
I updated the class names as suggested by the comment
For clarification: I used pure virtual in the class A to force any of the children to implement the functions.
It seems virtual inheritance is what I need, however, I don't seem to get the correct way on how to do this in my case...
The goal is to have several "base" classes, kind of like interfaces, forcing the children to implement the functions, but any children of those should inherit the overriden function (just like virtual inheritance)
However, using any combination of
class Any : public virtual Anyother { }
doesn't work out and always gives the same compile error (the one above). Perhaps I need to change more than just the virtual in the inheritance?
It doesn't work that way by default in C++ - You want a diamond inheritance pattern, but in C++ you get separate roots: So BasicClassA and BaseClassB each have their own BaseClassA (vtable and instance variables).
You probably want to use Virtual Inheritance.
For a clearer idea on non-virtual inheritance:
#include <iostream>
class A
{
public:
A(int x) {m_a = x;}
virtual ~A() {}
int m_a;
virtual int getA() {return m_a;}
};
class B : public A
{
public:
B() : A(1) {}
};
class C : public A
{
public:
C() : A(2) {}
};
class D : public B,
public C
{
};
void useB(B* b)
{
std::cout << "useB:" << b->getA() << std::endl;
}
void useC(C* c)
{
std::cout << "useC:" << c->getA() << std::endl;
}
int main()
{
D* d = new D();
useB(d);
useC(d);
return 0;
}
This produces the output:
useB:1
useC:2
This example shows virtual inheritance, and the kind of mix-in behaviour you want.
#include <iostream>
class A
{
public:
A(int x) {m_a = x;}
virtual ~A() {}
int m_a;
virtual int getA() {return m_a;}
virtual int virt() = 0;
};
class B : virtual public A
{
public:
B() : A(1) {}
};
class C : virtual public A
{
public:
C() : A(2) {}
virtual int virt() {return 42;}
};
class D : public B,
public C
{
public:
D() : A(3) {}
};
void useB(B* b)
{
std::cout << "useB:" << b->getA() << std::endl;
}
void useC(C* c)
{
std::cout << "useC:" << c->getA() << std::endl;
std::cout << "useC-virt:" << c->virt() << std::endl;
}
int main()
{
D* d = new D();
useB(d);
useC(d);
return 0;
}
Output:
useB:3
useC:3
useC-virt:42
Note: The constructors from C and B don't get a say in setting m_a, which is controller by the D() constructor initialisation list.
EDIT:
Applying virtual to your code:
#include <iostream>
class A
{
protected:
virtual std::string getDateTime() = 0;
virtual void Write(std::string data, bool addDate) = 0;
virtual bool CheckFile() = 0;
virtual bool OpenFile(std::string path) = 0;
virtual void CloseFile() = 0;
};
class B
: virtual public A
{
public:
virtual std::string ToString() { return ""; };
virtual void Write(std::string data) { };
};
class C
: virtual public A
{
protected:
std::string getDateTime()
{
return "TODAY";
};
void Write(std::string data, bool addDate)
{
std::cout << "C Write" << std::endl;
};
bool CheckFile()
{
std::cout << "C CheckFile" << std::endl;
return true;
};
bool OpenFile(std::string path)
{
std::cout << "C OpenFile" << std::endl;
return true;
};
void CloseFile()
{
std::cout << "C CloseFile" << std::endl;
};
};
class D
: public B,
public C
{
public:
std::string ToString()
{
return "D tostring";
};
void Write(std::string data)
{
std::cout << "D Write" << std::endl;
};
};
int main(int ac, char *av[])
{
D b;
std::cout << b.ToString() << std::endl;
b.Write("");
return 0;
}
BaseClassA has 5 pure virtual functions. A class with even one pure virtual function is an "Abstract class". The purpose of pure virtual functions (in short) is to disallow creation of objects of the abstract class.
In order to instantiate BaseClassB, it needs to have definitions of all 5 functions which you declared pure virtual in BaseClassA. (In absence of these definitions, BaseClassB also becomes Abstract and hence you cannot create objects from it).
BasicClassB only derives from BaseClassA which is an abstract class since those methods :
virtual std::string getDateTime() = 0;
virtual void Write(std::string data, bool addDate) = 0;
virtual bool CheckFile() = 0;
virtual bool OpenFile(std::string path) = 0;
virtual void CloseFile() = 0;
Are pure virtual.
The error message is pretty clear: to be able to instantiate a BasicClassB you must provide an implementation for the forementioned methods.
Also, note that your definition of Write in BasicClassB:
virtual void Write(std::string data) { };
Differs from the one in BaseClassA:
virtual void Write(std::string data, bool addDate) = 0;
So this method still needs to be implemented for BasicClassB to become instantiable.
The fact that you add "=0" to your functions means that they are purely virtual, and must be implemented in child classes. Which is obviously not what you want. If you drop the "=0" from the functions that have an implementation in the base class, it should be working as intended.