Method which returns vector on unknown type

P

pocmatos

Hi all,

Is it possible to have a class in which a method in some cases returns
a vector<int> and on others returns vector<double>? How?

Thanks,

Paulo Matos
 
Z

Zara

Hi all,

Is it possible to have a class in which a method in some cases returns
a vector<int> and on others returns vector<double>? How?

Thanks,

Paulo Matos

You can only have such functions if there is a base class common to
all types of results. So the answer is 'No'.
 
A

Axter

Hi all,

Is it possible to have a class in which a method in some cases returns
a vector<int> and on others returns vector<double>? How?

Thanks,

Paulo Matos

Either you need to have a common base class, or you need to have types
that have a common interface.

See following example for Heterogeneous Containers that can store
objects that have different base types, but have some type of common
interface (function signature).

http://code.axter.com/HeterogeneousContainer1.cpp
http://code.axter.com/HeterogeneousContainer2.cpp
http://code.axter.com/HeterogeneousContainer3.cpp
 
E

Eric Pruneau

Hi all,

Is it possible to have a class in which a method in some cases returns
a vector<int> and on others returns vector<double>? How?

Thanks,

Paulo Matos

You can use boost::any

Your method will return a vector<boost::any>
which can be .... anything!

Eric
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top