Returning pointer to derived form a base class function

Apr 3, 2015 at 8:32am
Is that posibble with covariant return types ? Namely that code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Derived;
class Base
{
public:
	virtual Derived* clone() const
	{
		return new Derived;
	}
};

class Derived : public Base{
public:
	Derived() {};
	virtual Derived* clone() const
	{
		return new Derived(*this);
	}
};

#include <iostream>

int main()
{
	Base ob;
	Derived od;
        return 0;
}


fails to compile and complains that no relevant Derived() constructor exist
Apr 3, 2015 at 8:41am
It fails because the Derived class is not defined when you try to create the object. You need to define Base::clone() after the Derived class has been defined.
Apr 3, 2015 at 9:39am
Thanks, it works now. Sometimes it seems that I lack such basic knowledge, eh..
Topic archived. No new replies allowed.