B
Bob Hairgrove
Consider the classic clone() function:
class A {
public:
virtual ~A() {}
virtual A* clone() const = 0;
};
class B : public A {
public:
// overrides A::clone() due to covariant return:
B* clone() const { return new B(*this); }
};
// etc.
If I am storing the return value of clone() in a container of pointers
to A, this buys me nothing, because there is an implicit conversion
from B* to A*. Indeed, with compilers which do not support covariant
return types, I can do exactly the same by declaring the return type
of B::clone() as A*.
I'm sure there was a reason for allowing this, but I can't seem to
come up with a concrete example of how I need this to do something I
couldn't do on a compiler which doesn't implement it.
--
Bob Hairgrove
(e-mail address removed)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
class A {
public:
virtual ~A() {}
virtual A* clone() const = 0;
};
class B : public A {
public:
// overrides A::clone() due to covariant return:
B* clone() const { return new B(*this); }
};
// etc.
If I am storing the return value of clone() in a container of pointers
to A, this buys me nothing, because there is an implicit conversion
from B* to A*. Indeed, with compilers which do not support covariant
return types, I can do exactly the same by declaring the return type
of B::clone() as A*.
I'm sure there was a reason for allowing this, but I can't seem to
come up with a concrete example of how I need this to do something I
couldn't do on a compiler which doesn't implement it.
--
Bob Hairgrove
(e-mail address removed)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]