Is there a vector base class?

Discussion in 'C++' started by Frank Bormann, Dec 1, 2004.

  1. Hi guys,

    I'm a C++ beginner. I was wondering if there is a generalized vector base
    class, through which I could pass a pointer to any kind of vector to a
    function, regardless of what type of objects the passed vector is holding.

    Regards,
    Frank
     
    Frank Bormann, Dec 1, 2004
    #1
    1. Advertising

  2. * Frank Bormann:
    >
    > I'm a C++ beginner. I was wondering if there is a generalized vector base
    > class, through which I could pass a pointer to any kind of vector to a
    > function, regardless of what type of objects the passed vector is holding.


    No.

    You can use a template function


    template< typename T >
    void f( std::vector<T> const& v )
    {
    ...
    }

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 1, 2004
    #2
    1. Advertising

  3. Frank Bormann wrote:
    > Hi guys,
    >
    > I'm a C++ beginner. I was wondering if there is a generalized vector base
    > class, through which I could pass a pointer to any kind of vector to a
    > function, regardless of what type of objects the passed vector is holding.
    >
    > Regards,
    > Frank


    Sorry, but there is nothing like what you want.
    However, you should take a look at this C++ FAQ:
    [31.3] How can I build a <favorite container> of objects of different types?
    The FAQ URL is listed in my signature.

    Also try searching the newsgroups and the web for
    "heterogeneous containers C++"

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Dec 1, 2004
    #3
  4. Frank Bormann

    Jeff Flinn Guest

    "Frank Bormann" <> wrote in message
    news:41adcc0f$0$24343$...
    > Hi guys,
    >
    > I'm a C++ beginner. I was wondering if there is a generalized vector base
    > class, through which I could pass a pointer to any kind of vector to a
    > function, regardless of what type of objects the passed vector is holding.



    Are you aware of iterators and generic algorithms?

    Provide a more concrete example of what your trying to accomplish, and I'm
    sure you'll get much better response.

    Jeff F
     
    Jeff Flinn, Dec 1, 2004
    #4
  5. Frank Bormann

    Freenet Guest

    "Jeff Flinn" <> wrote in message
    news:cokl50$ctd$...
    >
    > Provide a more concrete example of what your trying to accomplish, and I'm
    > sure you'll get much better response.


    Ok, sorry about that. I want to make parser that needs to parse data into
    different types of objects, both single objects and arrays (or vectors) of
    objects. An "object" can thereby also be a single int value or a C++ string.
    Basically I need to do something like this:

    #include <iostream>
    #include <vector>
    using namespace std;

    class A { };

    class B : public A { };

    template <typename T> void somefunc(T& data) { cout << "single template" <<
    endl; }

    void somefunc(A& data) { cout << "single A" << endl; }

    template <typename T> void somefunc(vector<T>& data) { cout << "vector
    template" << endl; }

    void somefunc(vector<A>& data) { cout << "vector A" << endl; }

    int main()
    {
    int test1;
    A test2;
    B test3;
    vector<int> test4;
    vector<A> test5;
    vector<B> test6;
    somefunc(test1);
    somefunc(test2);
    somefunc(test3);
    somefunc(test4);
    somefunc(test5);
    somefunc(test6);
    return 0;
    }

    Now, the output of this program is:

    single template
    single A
    single template
    vector template
    vector A
    vector template

    This means for the class B, which is derived from A, the compiler calls the
    template, but to accomplish what I want, I would need it to call the
    overloaded function for class A for all objects, that are derived from A. As
    you can imagine, I want to use the template for all build-in types & C++
    Strings and the overloaded class A versions for the classes, I define
    myself. Basically, I would need the output from the above program to be:

    single template
    single A
    single A
    vector template
    vector A
    vector A

    Frank
     
    Freenet, Dec 2, 2004
    #5
  6. "Freenet" <> wrote in message
    news:41aef58e$0$28873$...

    Sorry, bad newsreader config.
     
    Frank Bormann, Dec 2, 2004
    #6
  7. Frank Bormann

    Ron Natalie Guest

    Freenet wrote:

    >
    > This means for the class B, which is derived from A, the compiler calls the
    > template, but to accomplish what I want, I would need it to call the
    > overloaded function for class A for all objects, that are derived from A. As
    > you can imagine, I want to use the template for all build-in types & C++
    > Strings and the overloaded class A versions for the classes, I define
    > myself. Basically, I would need the output from the above program to be:


    You can't do it. There's no conversion from vector<Derived> to vector<Base>
    for the same reasons as there is no conversion between Derived[] to Base[].

    There's probably some magic you could do, but one thing I could think of
    is to add an generic "vector of A and it's derivatives" wrapper class to your app.
     
    Ron Natalie, Dec 2, 2004
    #7
  8. Frank Bormann

    Jeff Flinn Guest

    Frank,

    "Freenet" <> wrote in message
    news:41aef58e$0$28873$...
    > "Jeff Flinn" <> wrote in message
    > news:cokl50$ctd$...
    > >
    > > Provide a more concrete example of what your trying to accomplish, and

    I'm
    > > sure you'll get much better response.

    >
    > Ok, sorry about that. I want to make parser that needs to parse data into
    > different types of objects, both single objects and arrays (or vectors) of


    For parsing, you should definitely look at the boost.spirit parsing
    framework from www.boost.org. Although I don't readily see how your code
    below relates to parsing. Boost also now has a serialization library as well
    if this parsing is actually part of serialization process. Additionally,
    there have been a few std container io libraries recently proposed on the
    boost development mailing list.

    > objects. An "object" can thereby also be a single int value or a C++

    string.
    > Basically I need to do something like this:
    >
    > #include <iostream>
    > #include <vector>
    > using namespace std;
    >
    > class A { };
    >
    > class B : public A { };
    >
    > template <typename T> void somefunc(T& data) { cout << "single template"

    <<
    > endl; }
    >
    > void somefunc(A& data) { cout << "single A" << endl; }
    >
    > template <typename T> void somefunc(vector<T>& data) { cout << "vector
    > template" << endl; }
    >
    > void somefunc(vector<A>& data) { cout << "vector A" << endl; }
    >
    > int main()
    > {
    > int test1;
    > A test2;
    > B test3;
    > vector<int> test4;
    > vector<A> test5;
    > vector<B> test6;
    > somefunc(test1);
    > somefunc(test2);
    > somefunc(test3);
    > somefunc(test4);
    > somefunc(test5);
    > somefunc(test6);
    > return 0;
    > }
    >
    > Now, the output of this program is:
    >
    > single template
    > single A
    > single template
    > vector template
    > vector A
    > vector template
    >
    > This means for the class B, which is derived from A, the compiler calls

    the
    > template, but to accomplish what I want, I would need it to call the
    > overloaded function for class A for all objects, that are derived from A.

    As
    > you can imagine, I want to use the template for all build-in types & C++
    > Strings and the overloaded class A versions for the classes, I define
    > myself. Basically, I would need the output from the above program to be:
    >
    > single template
    > single A
    > single A
    > vector template
    > vector A
    > vector A


    I've no readily available answer, but places you may find a solution are in
    Modern C++ (Alexandrescu) in the Chapters dealing with acyclic visitors and
    multiple dispatch. Also there may be some facilities in the boost.mpl
    (meta-programming-library) that would help you accomplish your goals.

    Jeff Flinn

    Andrei
     
    Jeff Flinn, Dec 2, 2004
    #8
    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. CD
    Replies:
    2
    Views:
    825
    Victor Bazarov
    Oct 5, 2004
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    574
    John Carson
    Sep 3, 2005
  3. Replies:
    8
    Views:
    2,007
    Csaba
    Feb 18, 2006
  4. Hicham Mouline
    Replies:
    1
    Views:
    631
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    156
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page