Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   how to return different data types using a base class and pure virtual methods (http://www.velocityreviews.com/forums/t283797-how-to-return-different-data-types-using-a-base-class-and-pure-virtual-methods.html)

Mitch Mooney 06-17-2004 11:39 PM

how to return different data types using a base class and pure virtual methods
 
Subject line says it all. For example:

//base class
class foo{

public:
foo();
~foo();
virtual ??? GetValue()=0;
};

class bar1: public foo{
public:
bar1();
~bar1();
virtual char* GetValue(){return mValue;};
private:
char *mValue;
};

class bar2: public foo{
public:
bar1();
~bar1();
virtual int GetValue(){return mValue;};
private:
int mValue;
};

How do I return the value regardless of the data type? Ive tried using a
templated base class and could'nt complie. How can I accomplish this or
whats the work around?



Victor Bazarov 06-18-2004 01:33 AM

Re: how to return different data types using a base class and pure virtual methods
 
"Mitch Mooney" <mr.mhmooneyidontwantspam@verizon.net> wrote...
> Subject line says it all. For example:
>
> //base class
> class foo{
>
> public:
> foo();
> ~foo();
> virtual ??? GetValue()=0;
> };
>
> class bar1: public foo{
> public:
> bar1();
> ~bar1();
> virtual char* GetValue(){return mValue;};
> private:
> char *mValue;
> };
>
> class bar2: public foo{
> public:
> bar1();
> ~bar1();
> virtual int GetValue(){return mValue;};
> private:
> int mValue;
> };
>
> How do I return the value regardless of the data type? Ive tried using a
> templated base class and could'nt complie. How can I accomplish this or
> whats the work around?


There is no clear work-around. However, the usual advice in this case
is "do not do that". If you think you need this, you're probably not
modelling your world correctly.

In Java where every class is a descendant of Object, you would return
Object. Of course, you could try defining that class, but you will
need the support of the entire virtual machine for that. And that is
for making sure you can return anything from your function.

A better way would be to limit yourself to a few types you want to
cover and define some kind of 'VARIANT' (like COM has). Read up on it.

An even better way would be to rethink why you need that and probably
you won't need that after careful consideration.

No matter what you choose, good luck!

Victor



Bob Hairgrove 06-18-2004 07:52 AM

Re: how to return different data types using a base class and pure virtual methods
 
On Thu, 17 Jun 2004 23:39:15 GMT, "Mitch Mooney"
<mr.mhmooneyidontwantspam@verizon.net> wrote:

[snip]

>How do I return the value regardless of the data type? Ive tried using a
>templated base class and could'nt complie. How can I accomplish this or
>whats the work around?


You could derive a template class (or several) from your non-templated
abstract base class. I actually did this once for abstracting a
database row of data, each column having a possibly different type.
Since you often have a choice of returning a pointer or an actual
value, it might be a good idea to create two templated derived
classes, one set up for pointer semantics and the other for objects.

Your "GetValue()" function should return a void pointer if you want it
to work for all types. Of course, to use it for anything, you would
have to cast it to the appropriate type, so it is of limited use IMHO.
At the very least, you need to hide such details from clients by
implementing conversion functions -- or else proide a complete set of
"Get...()" functions (e.g.GetInt(), GetDouble(), etc.) -- or return a
proxy object from operator[], for example, which handles the
conversions transparently.

Not a trivial undertaking, and it's still going to be messy at best.


--
Bob Hairgrove
NoSpamPlease@Home.com


All times are GMT. The time now is 09:35 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.