Is there a vector base class?

F

Frank Bormann

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
 
A

Alf P. Steinbach

* 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 )
{
...
}
 
T

Thomas Matthews

Frank said:
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
 
J

Jeff Flinn

Frank Bormann said:
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
 
F

Freenet

Jeff Flinn said:
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
 
R

Ron Natalie

Freenet said:
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.
 
J

Jeff Flinn

Frank,

Freenet said:
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
 

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

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top