Curiosity on covariant return type (C++)
Hi, just curious, when I tried this trick on function returning referenceg++ failed. Also, when I searched in Google, all examples use functions that return a pointer. class Base { virtual Base& getRef() const = 0; virtual Base* getPtr() const = 0; } class Derived : public Base{ virtual Derived& getRef() const; //This one does not work. virtual Derived* getPtr() const; } Perhaps someone knows the exact definition (standard/ISO) of this -- is reference allowed or only pointer allowed? TIA. -- -- Verdi March --
On Wednesday 02 July 2003 14:55, Verdi March wrote:
Hi,
just curious, when I tried this trick on function returning referenceg++ failed. Also, when I searched in Google, all examples use functions that return a pointer.
class Base { virtual Base& getRef() const = 0; virtual Base* getPtr() const = 0; }
class Derived : public Base{ virtual Derived& getRef() const; //This one does not work. virtual Derived* getPtr() const; }
Perhaps someone knows the exact definition (standard/ISO) of this -- is reference allowed or only pointer allowed?
TIA.
I don't know the ISO defn, but does returning a const ref make any sense? I'd guess the constness would to apply to the reference itself, rather than the data it refers to, which probably isn't what you'd want. But you've got me wondering now... Arthur
On Friday 04 July 2003 04:04, Arthur Magill wrote:
I don't know the ISO defn, but does returning a const ref make any sense? I'd guess the constness would to apply to the reference itself, rather than the data it refers to, which probably isn't what you'd want.
It doesn't return const ref, it's a const method returning a non-const reference/pointer. Besides, reference is always constant -- a reference can't be force to refer/point to another "location", but the real data may or may not be constant. -- -- Verdi March --
participants (2)
-
Arthur Magill
-
Verdi March