472,358 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

polymorphic return types in virtual functions

If I have something like this:

class NumberException {
};

class Number {
public:
...
virtual unsigned long getValue() {throw(NumberException);};
...
};

class Integer : public Number {
public:
...
virtual int getValue() {return Value;};

Integer(int i) {Value=i;};
...

private:
int Value;
};

class Float : public Number {
public:
....
virtual float getValue() {return Value};

Float(float f);
....

private:
float Value;
};

int main()
{
Float f(3.14);
Integer i(55);

cout<<f->getValue()<<" "<<i->getValue()<<endl;
}

GCC (and likelly any other ANSI compilor) barfs on getValue having different
return types while being virtual... is there any way around this problem...
the idea is I want to do a "everything is an object" style paradigm (ala
Ruby).

--Aryeh
Jul 23 '05 #1
2 3819
On 2005-02-20, Aryeh M. Friedman <ar***@m-net.arbornet.org> wrote:
If I have something like this:

class Number {
public:
virtual unsigned long getValue() {throw(NumberException);};
};

class Integer : public Number {
public:
virtual int getValue() {return Value;}; class Float : public Number {
public:
virtual float getValue() {return Value};
Not clear on what the above is trying to achieve .. but hopefully this will
help:

Here's the problem -- consider this:

Number* x = new Integer(0);
x->getValue(); // what type does this return ?

Everything needs to return a certain type.

If you want a function to return multiple types, you need to hide the type
behind a dynamic pointer, e.g.

// always returns Number*, but the underlying object could be an Integer or
// Float
Number* f(int i, bool j) {
if (j)
return new Float(i);
else
return new Integer(i);
}
int main()
{
Float f(3.14);
Integer i(55);

cout<<f->getValue()<<" "<<i->getValue()<<endl;


This is incorrect code. The correct way to write this would be something like

int main()
{
Number* f = new Float(3.14);
Number* i = new Integer(55);
std::cout << *f << " " << *i << std::endl;
}

Then you need to overload operator<<.

e.g.

class Number {
public:
virtual std::string tostring = 0;
};

std::ostream& operator<< ( std::ostream& s, const Number& n) {
s << n.tostring();
}

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #2
Aryeh M. Friedman wrote:

GCC (and likelly any other ANSI compilor) barfs on getValue having different
return types while being virtual... is there any way around this problem...
the idea is I want to do a "everything is an object" style paradigm (ala
Ruby).

You can only have covariant return types from virtual functions when one
is a base class of the other. It's more specific than just being "convertable"
to the other.

The numerical types in C++ are not polymorphic. You could do it like this:

class Number {
unsigned long GetValue() { return getValueUL(); } // Not virtual.
virtual unsigned long GetValueUL() { return Value; }
};

class Float {
float GetValue() { return Value; }
unsigned long GetValueUL() { return Value(); } // definite covnersion.
};
Jul 23 '05 #3

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

Similar topics

20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
1
by: verec | last post by:
Last week I asked here how I could detect that a T was polymorphic, and received very thoughtful and useful replies that I used straight away. Thanks to all who answered. This week, it turns...
9
by: Karel Miklav | last post by:
In lots of places in a programm I need to identify type of received messages, so I create them as virtual classes and use RTTI to find their type later. But these are simple messages, often without...
23
by: Randy | last post by:
Since these operators can't be member functions, and since friend functions can't be declared virtual, how do I make my inserters and extractors polymorphic? --Randy Yates
5
by: FefeOxy | last post by:
Hi, > I'm having a debug assertion error within the file dbgdel.cpp with the expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) I traced the origin of the error and it happened as I tried to...
5
by: Ben Pope | last post by:
Hi all, This is not something I've played around with much, but I'm designing some factories and I want a function like this: template<class T> T* Creator() { return new T; }
4
by: Osamede.Zhang | last post by:
Compiler store a pointer table in each object with virtual fuction to implement polymorphic,isn't it? But where is the pointer table? It seems that object only store a pointer to the table,the...
3
by: jacek.dziedzic | last post by:
Hello! Suppose I have a class base, with virtual methods and a virtual destructor and a bunch of classes, derived1, derived2, ... which publicly derive from base. I then have a pointer base*...
11
by: Angus | last post by:
I am developing a server which receives a range of different messages. There are about 12 different message types so I thought that a good idea would be to devise a class for each message type....
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

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.