Method with unknown return type

N

Nuno Barros

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.
In this class i need to create a method to get values from the column.
The problem is that for each type of column the return type will be
different.

I tried using a template like

template <typename T>
virtual T getValue(int index)

but this is not a good idea since everytime that i need to use an
object Column i need to specify a type : Column<int> for example.

Is there some way to do this without an template?

Using a virtual class how can i implement a method that has different
return types depending on the Daughter class?

Thanks in advance,

Nuno
 
H

Howard

Nuno Barros said:
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.
In this class i need to create a method to get values from the column.
The problem is that for each type of column the return type will be
different.

I tried using a template like

template <typename T>
virtual T getValue(int index)

but this is not a good idea since everytime that i need to use an
object Column i need to specify a type : Column<int> for example.

Is there some way to do this without an template?

Using a virtual class how can i implement a method that has different
return types depending on the Daughter class?

Thanks in advance,

Nuno

I'm wondering...how will you handle getting different return types? I mean,
you have to assign the return value to *something*, right? (Or use it in
some manner, otherwise what's the point?) So your calling code must know
the type expected. So why not use templates then?

Alternatively, you could return an object that contains the data you want,
perhaps which uses a union for the data. It could hold an indicator of the
type of data it holds, if you need that. That's what is done in COM
programming with the Variant type.

You could also return a char*, or even a void*. But I'm pretty sure that
you can't have virtual functions that are overridden by functions that
return a different type.

-Howard
 
C

Conrad Weyns

Nuno Barros said:
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.
In this class i need to create a method to get values from the column.
The problem is that for each type of column the return type will be
different.

I tried using a template like

template <typename T>
virtual T getValue(int index)


Have a look at Boost.Any http://www.boost.org/doc/html/
Conrad Weyns.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top