J
JKop
Been thinking about the following:
class Mammal
{
public:
virtual void Mate(Mammal &) = 0;
};
The above seems rational to me - if something is to qualify as a Mammal,
then it must be able to mate.
But... then you have
class Dog : public Mammal
{
public:
virtual void Mate(Mammal &)
{
//Wait a minute, I'm not mating unless
//it's with another dog!
}
};
Has anyone met this situation before? Obviously my first thought was:
virtual void Mate(Dog &);
but then that doesn't overload the base class function...
I know the following would work, but it seems a bit inefficent to me:
#include <typeinfo>
virtual void Mate(Mammal &mammal)
{
//Wait a minute, I'm not mating unless
//it's with another dog!
try
{
Dog& doggy = dynamic_cast<Dog&>(mammal);
//Now perform mating
}
catch( std::bad_cast )
{
}
}
Any thoughts on this?
-JKop
class Mammal
{
public:
virtual void Mate(Mammal &) = 0;
};
The above seems rational to me - if something is to qualify as a Mammal,
then it must be able to mate.
But... then you have
class Dog : public Mammal
{
public:
virtual void Mate(Mammal &)
{
//Wait a minute, I'm not mating unless
//it's with another dog!
}
};
Has anyone met this situation before? Obviously my first thought was:
virtual void Mate(Dog &);
but then that doesn't overload the base class function...
I know the following would work, but it seems a bit inefficent to me:
#include <typeinfo>
virtual void Mate(Mammal &mammal)
{
//Wait a minute, I'm not mating unless
//it's with another dog!
try
{
Dog& doggy = dynamic_cast<Dog&>(mammal);
//Now perform mating
}
catch( std::bad_cast )
{
}
}
Any thoughts on this?
-JKop