This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: egcs-1.1 and the C++ standard



> > Non-trivial covariant returns
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> Could you expand on this one? Does this mean that the Standard somehow
> supports overloading based on return types now?

He's talking about the feature that allows you to override a virtual
function that returns a pointer or reference to B by one that returns a
pointer or reference to D, where D is derived from B.  It's useful for
things like clone methods (sometimes called the "virtual copy constructor"
pattern):

class B {
public:
	...
	B* clone() const { return new B(*this);}
};

In the ARM days you'd have to write

class D : public B {
public:
	...
	B* clone() const { return new D(*this);}
};

and probably wind up doing downcasts in your code.

Now you can write

	...
	D* clone() const { return new D(*this);}

The idea is that the return value from clone will always match the static
type of the pointer or reference you call it on.

For single inheritance, this is pretty simple to implement; the
code comes out the same.  For multiple inheritance or virtual
base classes, there's more work (adding an offset, etc).





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]