how to return different data types using a base class and pure virtual methods

M

Mitch Mooney

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?
 
V

Victor Bazarov

Mitch Mooney said:
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
 
B

Bob Hairgrove

On Thu, 17 Jun 2004 23:39:15 GMT, "Mitch Mooney"

[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top