Re: Confusion between this and *this on website -- possible error
Alf P. Steinbach wrote:
* pauldepstein@att.net:
Below is pasted from a website:
QUOTE BEGINS HERE
HOWTO: Covariant Return Types in C++
20060406T0140
Covariant return types are a useful feature of the C++ language that
could save you from doing some extra, unnecessary work in a few com
mon situations. They can also make the intended purpose of your
functions more clear. This HOWTO explains what they are, how to use
them, a nd gives some common sample applications.
Consider a simple inheritance hierarchy such as that shown here:
Base
|
v
Derived
Often, the derived class has a polymorphic function which returns a
more specific (i.e. more derived) type than the base class. This is
per fectly legal, since the pointer is automatically downcast to the
base class type.
A common use of this (but by no means the only one) is a clone()
method:
class Base {
public:
virtual Base* clone() const
{
return new Base(this);
}
};
class Derived : public Base {
public:
virtual Base* clone() const
{
return new Derived(this);
}
};
QUOTE ENDS HERE
I would expect to see new Derived(*this) and
new Base(*this)
Right.
Is the website in error or am I in error?
The website.
On top of that, they're not even showing covariant return types.
They're showing simple virtual overriding.
It should be:
class Derived : public Base {
public:
virtual Derived* clone() const
{
return new Derived(*this);
}
};