473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Covariance Return in C++

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<intb;
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]'

Mar 28 '07 #1
2 4001
mu********@gmail.com wrote:
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 {};
There is no 'X' here. In templates base classes are not looked at
when resolving names. You need to prepend this definition with

using A<Object>::X;

or even

typedef typename A<Object>::X X;
virtual Y* f() {return new Y;}
};

int main(void) {
B<intb;
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]'
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 28 '07 #2
Thank you very much.

Mar 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave | last post by:
Hello all, I had thought that both return types and parameter types of virutal functions could be covariant. However, looking in the Standard (electronic search), I find reference to only...
1
by: Kalyan | last post by:
Hi All, I have the table as follows column1 ...
1
by: Tony Johansson | last post by:
Hello!! When you have result covariance is it then necessary that you have inheritance. I mean can you have result covariance without having inheritance. //Tony
0
by: Aryeh Holzer | last post by:
Let me start with a quote from the C# Programmers Reference (where I learned the cool word "covariance"): "When a delegate method has a return type that is more derived than the delegate...
1
by: Arkadiusz Stasiak | last post by:
I am looking for functions/library to calculate covariance matrix, eigenvectors, eigenvalues in Visual C++, as299@kent.ac.uk. Thanks
3
by: ollisalo | last post by:
Hi Firstly I'm quite a newbie in SQL, so apologies for any obvious stupid questions. My problem is that I have a large set of stock price data for different companies together with the stock...
0
by: rach | last post by:
I am testing the covariance return in C++. First the following code worked. class A { public: class X {}; virtual X* f() {return new X;} }; class B : public A {
1
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
Jon Skeet wrote in the .NET general newsgroup 11/17/2005: "The difference between Invoke and DynamicInvoke is that the parameters to Invoke depend on the delegate itself - the method has the same...
2
by: Anders Borum | last post by:
Hello! I was wondering if anybody in this newsgroup are following the status of covariance in C#? As far as I'm concerned, generics covariance is not supported with C# 3.0 - unfortunately, as...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.