M
mufenhsieh
I am testing covariance return in C++. First the following code
worked.
class A
{
public:
class X {};
virtual X* f() {return new X;}
};
class B : public A
{
public:
class Y : public X {};
virtual Y* f() {return new Y;}
};
int main(void) {
B b;
b.f();
}
However, after I modified it into template version, it couldn't
compile. Does anyone know how to make it work? Thanks.
template <typename Object>
class A
{
public:
class X {};
virtual X* f() {return new X;}
};
template <typename Object>
class B : public A<Object>
{
public:
class Y : public X {};
virtual Y* f() {return new Y;}
};
int main(void) {
B<int> b;
b.f();
}
Error messages:
test.cpp:20: error: expected class-name before '{' token
test.cpp: In instantiation of `B<int>':
test.cpp:25: instantiated from here
test.cpp:21: error: invalid covariant return type for `B<Object>::Y*
B<Object>::f() [with Object = int]'
test.cpp:11: error: overriding `A<Object>::X* A<Object>::f() [with
Object = int]'
worked.
class A
{
public:
class X {};
virtual X* f() {return new X;}
};
class B : public A
{
public:
class Y : public X {};
virtual Y* f() {return new Y;}
};
int main(void) {
B b;
b.f();
}
However, after I modified it into template version, it couldn't
compile. Does anyone know how to make it work? Thanks.
template <typename Object>
class A
{
public:
class X {};
virtual X* f() {return new X;}
};
template <typename Object>
class B : public A<Object>
{
public:
class Y : public X {};
virtual Y* f() {return new Y;}
};
int main(void) {
B<int> b;
b.f();
}
Error messages:
test.cpp:20: error: expected class-name before '{' token
test.cpp: In instantiation of `B<int>':
test.cpp:25: instantiated from here
test.cpp:21: error: invalid covariant return type for `B<Object>::Y*
B<Object>::f() [with Object = int]'
test.cpp:11: error: overriding `A<Object>::X* A<Object>::f() [with
Object = int]'