Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Intermediate
Apr 17, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Return type of an overriding virtual member function

Once declared virtual, a member function can be overridden in any level of derivation, as long as the overriding version has the identical signature of the original declaration. This is quite straightforward, but sometimes a virtual has to return an object of type that differs from the return type declared in its base class. The C++ Standard allows that only when the return type is replaced with a class publicly derived from the original return type. For example:
class B {
	virtual B* clone () const { return new B; }

class D {
	void* clone () const { return new B; } //error; return type
D* clone () const { return new D; } //OK, D is publicly
//derived from B
Danny Kalev
Thanks for your registration, follow us on our social networks to keep up-to-date