| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Nuno Barros" |
| Date: |
19 Sep 2003 05:59:32 AM |
| Object: |
Method with unknown return type |
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
.
|
|
| User: "Howard" |
|
| Title: Re: Method with unknown return type |
19 Sep 2003 08:55:11 AM |
|
|
"Nuno Barros" <nuno.barros@fc.ul.pt> wrote in message
news:a8970ae1.0309190259.3323a712@posting.google.com...
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
.
|
|
|
|
| User: "Conrad Weyns" |
|
| Title: Re: Method with unknown return type |
19 Sep 2003 09:31:14 AM |
|
|
"Nuno Barros" <nuno.barros@fc.ul.pt> wrote in message
news:a8970ae1.0309190259.3323a712@posting.google.com...
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.
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
.
|
|
|
|

|
Related Articles |
|
|