| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Frank Bormann" |
| Date: |
01 Dec 2004 07:50:11 AM |
| Object: |
Is there a vector base class? |
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
.
|
|
| User: "Jeff Flinn" |
|
| Title: Re: Is there a vector base class? |
01 Dec 2004 08:40:31 AM |
|
|
"Frank Bormann" <news.1.fbormann@xoxy.net> wrote in message
news:41adcc0f$0$24343$9b622d9e@news.freenet.de...
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
.
|
|
|
| User: "Freenet" |
|
| Title: Re: Is there a vector base class? |
02 Dec 2004 04:48:40 AM |
|
|
"Jeff Flinn" <NONONE@nowhere.com> wrote in message
news:cokl50$ctd$1@bluegill.adi.com...
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
.
|
|
|
| User: "Frank Bormann" |
|
| Title: Re: Is there a vector base class? |
02 Dec 2004 04:50:50 AM |
|
|
"Freenet" <news.1.fbormann@xoxy.net> wrote in message
news:41aef58e$0$28873$9b622d9e@news.freenet.de...
Sorry, bad newsreader config.
.
|
|
|
|
| User: "Jeff Flinn" |
|
| Title: Re: Is there a vector base class? |
02 Dec 2004 01:40:13 PM |
|
|
Frank,
"Freenet" <news.1.fbormann@xoxy.net> wrote in message
news:41aef58e$0$28873$9b622d9e@news.freenet.de...
"Jeff Flinn" <NONONE@nowhere.com> wrote in message
news:cokl50$ctd$1@bluegill.adi.com...
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
.
|
|
|
|
| User: "Ron Natalie" |
|
| Title: Re: Is there a vector base class? |
02 Dec 2004 09:40:28 AM |
|
|
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.
.
|
|
|
|
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: Is there a vector base class? |
01 Dec 2004 08:03:05 AM |
|
|
* 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?
.
|
|
|
|
| User: "Thomas Matthews" |
|
| Title: Re: Is there a vector base class? |
01 Dec 2004 08:10:12 AM |
|
|
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
.
|
|
|
|

|
Related Articles |
|
|