Determining Type and Extracting Data from private class member vector<boos::variant<> >

Hi all, I'm new to Boost and have some questions about how to get data out of a vector<boost::variant<> > container. For this purpose I put together 'class Base' which holds a vector<boost::variant <Base*, DerivedBase*> > Pointers. I wrote a function template to insert pointers, but I have real difficulty extracting pointers. For example, if I insert 3 Base* pointers and 2 DerivedBase* pointers, I need to be able to extract the 3 Base * pointers and the 2 DerivedBase* pointers at a later point in time. To this end, class Base* has another member function 'template< typename C> int numPointer( C * InPointer)' which is intended to count the number of pointers found in the container. template< typename C> int numPointer(C* InPointer) has a few problems: 1) typeid(Pointers[i]) returns the 'variant' typeid when I really need to know the typeid of the ith pointer in 'Pointers'. 2) There must be a better, more elegant (and safe) way to make use of the boost library to accomplish just what I want. int main() { Base * TempBase = new Base(); DerivedBase * TempDeri = new DerivedBase(); TempBase->insert(TempBase); TempBase->insert(TempBase); TempBase->insert(TempDeri); std::cout << "Base:" << TempBase->numPointer(TempBase) << endl; //Expect "Base:2" get "Base:0" because of typeid(). std::cout << "Deri:" << TempBase->numPointer(TempDeri) << endl; //Expect "Deri:1" get "Deri:0" because of typeid(). } class Base { private: vector<boost::variant <Base*, DerivedBase*> > Pointers; public: Base(); virtual ~Base(); template <typename C> void insert( C * inC ) { Pointers.push_back(inC); } template <typename C> int numPointer( C* inC ) { int counter(0); for ( int i(0); i < int(Pointers.size()); i++ ) { cout << "type:" << typeid(inC).name() << endl; cout << "typeP:" << typeid(Pointers[i]).name() << endl; //Will give the id of the container not Pointer[i]. //if( boost::is_same<Pointers[i],typeid(inC).name()>::value ){ //Compilation error if ( typeid(inC).name() == typeid(Pointers[i]).name() ) { counter++; } } return counter; } }; //Base Any advice on how to improve this code and/or make better use of the boost library is fully appreciated. Thanks, Matt

On Fri, 7 Jan 2005 14:07:14 -0600, Matthew Jankowski <mattjankowski@yahoo.com> wrote:
Hi all, I'm new to Boost and have some questions about how to get data out of a vector<boost::variant<> > container. For this purpose I put together 'class Base' which holds a vector<boost::variant <Base*, DerivedBase*> > Pointers. I wrote a function template to insert pointers, but I have real difficulty extracting pointers.
I'm not sure what you're trying to do makes much sense to do with Boost.Variant. It is more suited to storing objects than pointers. Why not just a std::vector<boost::shared_ptr<Base> >>? I'm assuming your DerivedBase object inherits from Base and you're trying to avoid slicing by storing pointers in your container. If you're insistent on using Variant, you can use get<T> to pull out a value by its type: #include <vector> #include <iostream> #include <algorithm> #include <boost/variant.hpp> using namespace std; using namespace boost; struct base {}; struct derived : public base {}; typedef variant<base*, derived*> holder; struct is_base { bool operator () (holder& h) const { return get<base*> (&h) != 0; } }; struct is_derived { bool operator () (holder& h) const { return get<derived*> (&h) != 0; } }; int main () { vector<holder> geoffrey; geoffrey.push_back (new base); geoffrey.push_back (new base); geoffrey.push_back (new base); geoffrey.push_back (new derived); geoffrey.push_back (new derived); cout << "base: " << count_if (geoffrey.begin (), geoffrey.end (), is_base ()) << ", derived: " << count_if (geoffrey.begin (), geoffrey.end (), is_derived ()) << endl; } -- Caleb Epstein caleb dot epstein at gmail dot com
participants (2)
-
Caleb Epstein
-
Matthew Jankowski