Method which returns vector on unknown type

Discussion in 'C++' started by pocmatos@gmail.com, Nov 11, 2005.

  1. Guest

    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
    , Nov 11, 2005
    #1
    1. Advertising

  2. Guest

    , Nov 11, 2005
    #2
    1. Advertising

  3. Zara Guest

    On 11 Nov 2005 00:45:21 -0800, wrote:

    >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'.
    Zara, Nov 11, 2005
    #3
  4. Guest

    Zara wrote:
    > On 11 Nov 2005 00:45:21 -0800, wrote:
    >
    > >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'.


    Oh. :(
    , Nov 11, 2005
    #4
  5. Axter Guest

    wrote:
    > 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
    Axter, Nov 11, 2005
    #5
  6. Eric Pruneau Guest

    <> a écrit dans le message de news:
    ...
    > 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
    Eric Pruneau, Nov 11, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Darrett

    a function which returns a vector

    Mike Darrett, Sep 16, 2004, in forum: C++
    Replies:
    6
    Views:
    9,669
  2. Replies:
    8
    Views:
    1,887
    Csaba
    Feb 18, 2006
  3. Noah Roberts
    Replies:
    1
    Views:
    381
    Noah Roberts
    Mar 15, 2010
  4. Vincent Arnoux
    Replies:
    1
    Views:
    230
    Arnaud Bergeron
    Aug 11, 2006
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    343
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page