how to return different data types using a base class and pure virtual methods

Discussion in 'C++' started by Mitch Mooney, Jun 18, 2004.

  1. Mitch Mooney

    Mitch Mooney Guest

    Subject line says it all. For example:

    //base class
    class foo{

    public:
    foo();
    ~foo();
    virtual ??? GetValue()=0;
    };

    class bar1: public foo{
    public:
    bar1();
    ~bar1();
    virtual char* GetValue(){return mValue;};
    private:
    char *mValue;
    };

    class bar2: public foo{
    public:
    bar1();
    ~bar1();
    virtual int GetValue(){return mValue;};
    private:
    int mValue;
    };

    How do I return the value regardless of the data type? Ive tried using a
    templated base class and could'nt complie. How can I accomplish this or
    whats the work around?
    Mitch Mooney, Jun 18, 2004
    #1
    1. Advertising

  2. "Mitch Mooney" <> wrote...
    > Subject line says it all. For example:
    >
    > //base class
    > class foo{
    >
    > public:
    > foo();
    > ~foo();
    > virtual ??? GetValue()=0;
    > };
    >
    > class bar1: public foo{
    > public:
    > bar1();
    > ~bar1();
    > virtual char* GetValue(){return mValue;};
    > private:
    > char *mValue;
    > };
    >
    > class bar2: public foo{
    > public:
    > bar1();
    > ~bar1();
    > virtual int GetValue(){return mValue;};
    > private:
    > int mValue;
    > };
    >
    > How do I return the value regardless of the data type? Ive tried using a
    > templated base class and could'nt complie. How can I accomplish this or
    > whats the work around?


    There is no clear work-around. However, the usual advice in this case
    is "do not do that". If you think you need this, you're probably not
    modelling your world correctly.

    In Java where every class is a descendant of Object, you would return
    Object. Of course, you could try defining that class, but you will
    need the support of the entire virtual machine for that. And that is
    for making sure you can return anything from your function.

    A better way would be to limit yourself to a few types you want to
    cover and define some kind of 'VARIANT' (like COM has). Read up on it.

    An even better way would be to rethink why you need that and probably
    you won't need that after careful consideration.

    No matter what you choose, good luck!

    Victor
    Victor Bazarov, Jun 18, 2004
    #2
    1. Advertising

  3. On Thu, 17 Jun 2004 23:39:15 GMT, "Mitch Mooney"
    <> wrote:

    [snip]

    >How do I return the value regardless of the data type? Ive tried using a
    >templated base class and could'nt complie. How can I accomplish this or
    >whats the work around?


    You could derive a template class (or several) from your non-templated
    abstract base class. I actually did this once for abstracting a
    database row of data, each column having a possibly different type.
    Since you often have a choice of returning a pointer or an actual
    value, it might be a good idea to create two templated derived
    classes, one set up for pointer semantics and the other for objects.

    Your "GetValue()" function should return a void pointer if you want it
    to work for all types. Of course, to use it for anything, you would
    have to cast it to the appropriate type, so it is of limited use IMHO.
    At the very least, you need to hide such details from clients by
    implementing conversion functions -- or else proide a complete set of
    "Get...()" functions (e.g.GetInt(), GetDouble(), etc.) -- or return a
    proxy object from operator[], for example, which handles the
    conversions transparently.

    Not a trivial undertaking, and it's still going to be messy at best.


    --
    Bob Hairgrove
    Bob Hairgrove, Jun 18, 2004
    #3
    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. qazmlp
    Replies:
    4
    Views:
    430
    David Harmon
    Feb 15, 2004
  2. Replies:
    10
    Views:
    720
    Richard Herring
    Oct 18, 2005
  3. Arne Schmitz
    Replies:
    4
    Views:
    428
    Daniel Albuschat
    Jan 17, 2007
  4. nw
    Replies:
    3
    Views:
    2,263
  5. Replies:
    2
    Views:
    553
Loading...

Share This Page