Connecting Tech Pros Worldwide Forums | Help | Site Map

Method with unknown return type

Nuno Barros
Guest
 
Posts: n/a
#1: Jul 19 '05
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

Howard
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Method with unknown return type



"Nuno Barros" <nuno.barros@fc.ul.pt> wrote in message
news:a8970ae1.0309190259.3323a712@posting.google.c om...[color=blue]
> 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[/color]

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




Conrad Weyns
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Method with unknown return type



"Nuno Barros" <nuno.barros@fc.ul.pt> wrote in message
news:a8970ae1.0309190259.3323a712@posting.google.c om...[color=blue]
> 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)[/color]


Have a look at Boost.Any http://www.boost.org/doc/html/
Conrad Weyns.
[color=blue]
>
> 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[/color]


Closed Thread