473,796 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Bi rd::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 4615
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<Bi rd::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.c om/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_bet a.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<Bi rd::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
5349
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 some daugther classes like ColumnInt, ColumnFLoat, etc etc. The problemis that i must access the data in the columns by using an Object (pointer) of the type Column.
3
510
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 course, the generated proxy is not the same). When I am looking at the C++ generated code, it seems fine, but when I am executing the code, I always get the first choice type.
0
1042
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 just trying to get a string return of "Error". I created the Runtime Callable Wrapper and referenced in my .NET project. I also registered the VB 6.0 .DLL
59
3464
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
1
1508
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 = FaxMsg.Recipients.Add(0) Recip.Name = sWho
9
4787
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 "ReturnSuper". This gives the following compile error due to a bad return type: TestSuper does not implement interface member TestBase.Func. TestSuper.Func is either static, not public, or has the wrong return type.
2
4905
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 I have a main form linked to a as400 table. This has a subform on it, also linked to a AS400 table. Now the foirst control on the subform( also the first field a user needs to complete on the form) is the one I am trying to popullate. The minute I...
3
6116
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 characters into the variant type, however when I use the 6.5 database I get only the limited number of characters that are actually in the column.
0
2094
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, size, qty, price,....) and here is the SQL of the subform: Code: SELECT purchase.purchase_num, purchase.Invoice_num, purchase.article, purchase.size, purchase.quantity, item.Price, item.Price_after_discount, purchase.unit_price,...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9531
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.