473,326 Members | 2,255 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,326 software developers and data experts.

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?
Jul 22 '05 #1
2 1743
"Mitch Mooney" <mr**********************@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
Jul 22 '05 #2
On Thu, 17 Jun 2004 23:39:15 GMT, "Mitch Mooney"
<mr**********************@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
No**********@Home.com
Jul 22 '05 #3

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

Similar topics

94
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
4
by: garyolsen | last post by:
class base{ public: void NonVirtualMethod(int a); virtual void VirtualMethod(int a); }; class derived{ public: void NonVirtualMethod(int a, char c); void VirtualMethod(int a, char c);
4
by: qazmlp | last post by:
I have a base class('base') containing 10 public pure virtual menthods. I have a class(derivedOne) deriving from 'base'. derivedOne implements only 7 of those base methods. Now, is it a good style...
8
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
14
by: Stefan Slapeta | last post by:
Hi, this code does not compile in C#: class base_class {} class derived_class : base_class {} class A { public virtual base_class f()
17
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
9
by: Alexander Widera | last post by:
hi, is it possible to return an object of an unknown (but not really unknown) type with an method? i have the following situation: - a variable (A) of the type "object" which contains the...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.