class IA
{
};
class IB : public IA
{
};
class B : public IB
{
};
class IX
{
public:
virtualvoid x(IA *a) = 0;
};
class X : public IX
{
public:
//virtual void x(IA *b) {}
virtualvoid x(IB *b)
{
cout << "ok" << endl;;
}
};
void main()
{
B b;
X x;
x.x(&b);
}
// http://ideone.com/EoMbuV
#include <iostream>
class IA
{
};
class IB : public IA
{
};
class B : public IB
{
};
class IX
{
public:
virtualvoid x(IA*) = 0;
};
class X : public IX
{
public:
virtualvoid x(IA*)
{
std::cout << "ok\n";
}
};
int main()
{
B b;
X x;
x.x(&b);
}
Many thanks for your reply cire:
In fact, in class IX, we want to design x such that its argument is IA* or T* where T is any type which is derived from IA:
1 2 3 4 5
class IX
{
public:
virtualvoid x(IA * or (Any type which extends IA)*) = 0;
};
class Rec_Tester : public ITester
{
public:
virtualvoid test(const IShape *pshape) const
{
const IRect* prect = dynamic_cast<const IRect*>(pshape);
assert(prect);
cout << "Shape is a rect\n"
<< "width = " << prect->getWidth() << "\n"
<< "height = " << prect->getHeight() << "\n";
}
};
class Circle_Tester : public ITester
{
public:
virtualvoid test(const IShape *pshape) const
{
const ICircle* pcirc = dynamic_cast<const ICircle*>(pshape);
assert(pcirc);
cout << "Shape is a circle\n"
<< "radius = " << pcirc->getRadius() << "\n";
}
};
Now, is it possible to check same in compiling-time like below-coming codes (without adding other virtual functions in Itester. it is no problem if the format of test function is changed)?
You can't derive from ITester and then change the type of parameter that its function test() takes. This is exactly the same issue which came up earlier, with IX's function x(), and you were told it was not possible. It's still not possible!
I am unclear why you want to use interfaces (assuming the I prefix is for interface.) Why do Rec_Tester and Circle_Tester need to derive from ITester?
To fully answer your question we really need to understand what underlying problem you're trying to solve with this approach.
cire has already mentioned operloading; that might just be a better approach. And then there are templates (and template specialization.) Etc.