I managed to isolate a problem I'm having to this example program:
Code:
#include <iostream>
using namespace std;
//Simple inheritance
class Parent {};
class Child: public Parent {};
//Functions
void foo(Parent &daddy)
{
cout << "Success!" << endl;
}
void evil(Child &object, void (*doodle)(Child &dimitri))
{
doodle(object);
}
int main()
{
Child object;
foo(object); //OK
evil(object, foo); //Compilation error
return 0;
}
Yes, I understand the obvious error. It's expecting a function with a Child& parameter, not Parent&. But the fact is, I have another class which is a custom template container. Something like this:
Code:
#include <iostream>
using namespace std;
//simple inheritance
class Parent{};
class Child: public Parent{};
//a simple template class
template <class Type>
class Container
{
public:
void evil(Type &object, void (*doodle)(Type &item));
};
template <class Type>
void Container<Type>::evil(Type &object, void (*doodle)(Type &item))
{
doodle(object);
}
void foo(Parent &item)
{
cout << "Success!" << endl;
}
int main()
{
Child object;//mkay
foo(object); //No error
Container<Child> box; //dundundun....
Container<Parent> box2; //dundundun....
box2.evil(object, foo); //No error
box.evil(object, foo); //Compile error!!!
return 0;
}
This second example won't work for the same reasons as the first.
As you can see, I wish for every class derived from Parent to be able to use that function. I don't want to overload the same code over and over again. I know that it took me way more time to conjure this post than it would if I overloaded the function for every derived class, but that's not the point :P I don't want it to just "work", I want to learn efficient ways to help me when I'll be writing more complex programs in the future.
Is there any way I can possibly do this? I'm thinking of a few abstract ways, but I'm lost.
Thank you for your time ^^