473,395 Members | 1,568 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,395 software developers and data experts.

Variant return type

C++
According to Thinking in C++
"You cannot modify the return type of a virtual function during
overriding.but there is a special case in which you can slightly
modify the return type. If you¡¯re returning a pointer or a reference to a
base class, then the overridden version of the function may
return a pointer or reference to a class derived from what the base
returns." And here's the example:

class PetFood
{
public:
virtual string foodType() const = 0;
};

class Pet
{
public:
virtual string type() const = 0;
virtual PetFood* eats() = 0;
};

class Bird : public Pet
{
public:
string type() const { return "Bird"; }
class BirdFood : public PetFood
{
public:
string foodType() const
{
return "Bird food";
}
};

// Upcast to base type:
PetFood* eats() { return &bf; }
private:
BirdFood bf;
};

class Cat : public Pet
{
public:
string type() const { return "Cat"; }
class CatFood : public PetFood
{
public:
string foodType() const { return "Birds"; }
};
// Return exact type instead:
CatFood* eats() { return &cf; }
private:
CatFood cf;
};

int main()
{
Bird b;
Cat c;
Pet* p[] = { &b, &c, };
for(int i = 0; i < sizeof p / sizeof *p; i++)
cout << p[i]->type() << " eats "
<< p[i]->eats()->foodType() << endl;
// Can return the exact type:
Cat::CatFood* cf = c.eats();
Bird::BirdFood* bf;
// Cannot return the exact type:
//! bf = b.eats();
// Must downcast:
bf = dynamic_cast<Bird::BirdFood*>(b.eats());
}

What annoying me is that once I get code above compiled, I get a compiler
error saying
"overriding virtual function differs from 'Pet::eats' only by return type or
calling convention" which is for "CatFood* eats() { return &cf; }"
But this differ is author's intension, How's that was flagged as an error?
Nov 27 '05 #1
3 4575
C++ wrote:
What annoying me is that once I get code above compiled, I get a compiler
error saying
"overriding virtual function differs from 'Pet::eats' only by return type or
calling convention" which is for "CatFood* eats() { return &cf; }"
But this differ is author's intension, How's that was flagged as an error?


Compiles well on g++3.4.2 and also on Comeau online.
Probably your compiler might have a bug.

Nov 27 '05 #2
* C++:
According to Thinking in C++
"You cannot modify the return type of a virtual function during
overriding.but there is a special case in which you can slightly
modify the return type. If you¡¯re returning a pointer or a reference to a
base class, then the overridden version of the function may
return a pointer or reference to a class derived from what the base
returns." And here's the example:

class PetFood
{
public:
virtual string foodType() const = 0;
};

class Pet
{
public:
virtual string type() const = 0;
virtual PetFood* eats() = 0;
};

class Bird : public Pet
{
public:
string type() const { return "Bird"; }
class BirdFood : public PetFood
{
public:
string foodType() const
{
return "Bird food";
}
};

// Upcast to base type:
PetFood* eats() { return &bf; }
private:
BirdFood bf;
};

class Cat : public Pet
{
public:
string type() const { return "Cat"; }
class CatFood : public PetFood
{
public:
string foodType() const { return "Birds"; }
};
// Return exact type instead:
CatFood* eats() { return &cf; }
private:
CatFood cf;
};

int main()
{
Bird b;
Cat c;
Pet* p[] = { &b, &c, };
for(int i = 0; i < sizeof p / sizeof *p; i++)
cout << p[i]->type() << " eats "
<< p[i]->eats()->foodType() << endl;
// Can return the exact type:
Cat::CatFood* cf = c.eats();
Bird::BirdFood* bf;
// Cannot return the exact type:
//! bf = b.eats();
// Must downcast:
bf = dynamic_cast<Bird::BirdFood*>(b.eats());
}

What annoying me is that once I get code above compiled, I get a compiler
error saying
"overriding virtual function differs from 'Pet::eats' only by return type or
calling convention" which is for "CatFood* eats() { return &cf; }"
But this differ is author's intension, How's that was flagged as an error?


It means you're using an old compiler.

FAQ item 20.8 (as the numbering is right now) describes covariant return
types, <url:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8>.

Section 1.5.3 of
<url:
http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01_beta.doc.pdf>
describes a workaround for older compilers such as (presumably) yours,
as well as discussing the issues in more detail & generality.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 27 '05 #3

C++ wrote:
According to Thinking in C++
"You cannot modify the return type of a virtual function during
overriding.but there is a special case in which you can slightly
modify the return type. If you¡¯re returning a pointer or a referenceto a
base class, then the overridden version of the function may
return a pointer or reference to a class derived from what the base
returns." And here's the example:

class PetFood
{
public:
virtual string foodType() const = 0;
};

class Pet
{
public:
virtual string type() const = 0;
virtual PetFood* eats() = 0;
};

class Bird : public Pet
{
public:
string type() const { return "Bird"; }
class BirdFood : public PetFood
{
public:
string foodType() const
{
return "Bird food";
}
};

// Upcast to base type:
PetFood* eats() { return &bf; }
private:
BirdFood bf;
};

class Cat : public Pet
{
public:
string type() const { return "Cat"; }
class CatFood : public PetFood
{
public:
string foodType() const { return "Birds"; }
};
// Return exact type instead:
CatFood* eats() { return &cf; }
private:
CatFood cf;
};

int main()
{
Bird b;
Cat c;
Pet* p[] = { &b, &c, };
for(int i = 0; i < sizeof p / sizeof *p; i++)
cout << p[i]->type() << " eats "
<< p[i]->eats()->foodType() << endl;
// Can return the exact type:
Cat::CatFood* cf = c.eats();
Bird::BirdFood* bf;
// Cannot return the exact type:
//! bf = b.eats();
// Must downcast:
bf = dynamic_cast<Bird::BirdFood*>(b.eats());
}

What annoying me is that once I get code above compiled, I get a compiler
error saying
"overriding virtual function differs from 'Pet::eats' only by return typeor
calling convention" which is for "CatFood* eats() { return &cf; }"
But this differ is author's intension, How's that was flagged as an error?


What compiler are you using? Comeau online compiles your code fine if I
add

#include <iostream>
#include <string>
using namespace std;

at the beginning. If you are using an old compiler you may have the
problem described here:

http://www.parashift.com/c++-faq-lit....html#faq-20.8

Gavin Deane

Nov 27 '05 #4

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

Similar topics

2
by: Nuno Barros | last post by:
Hello, I am writting a c++ code to crete a kind of a table in memory. Each table has an container of columns, which can be of any type. To do this i created a virtual class Column which has...
3
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of...
0
by: Kurt Kirkham | last post by:
I am trying to get a variant return in C# from a VB 6.0 .dll. The VB 6.0 .dll has a method signiture of: Public Function GetStateCodes() As Variant This is located in ElecPub.dll Data. I am...
59
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
1
by: darrenbenn | last post by:
I need to convert this code to VB .NET (2003). Dim FaxMsg As IFaxMessage Set FaxMsg = gFC.NewMessage Dim Recip As Variant ' add a recipient to the fax recipients collection Set Recip =...
9
by: hufaunder | last post by:
I have a class "TestSuper" that implements the interface "TestBase". The interface has a property of type "ReturnType". The class "TestSuper" does not return "ReturnType" but a derivation...
2
by: bloukopkoggelmander | last post by:
Hi all I am getting the following error and it is driving me insane. I just cannot find a fix for it. : You tried to assign the NULL value to a variable that is not a Variant data type. Now...
3
by: empire5 | last post by:
I'm trying to convert a MS-Sql 6.5 VB application to SQL 2005 and vb.net. The vb app has uses a variant data type. When I try to read the variant data type from the sql 2005 database I get 8,000...
0
by: hannoudw | last post by:
Hi. I'm working with a form that contain text boxes ( Invoice_num auto number, and Invoice_date bound to the table Invoice) , and a subform contains (num (and it's an auto number also) , article,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.