Conversion of one object type to another

R

Ram

Hi,

I have a container of base pointers and I want to generate a container
of derived class pointers from it by casting each pointer of base class
to a pointer of derived class. Is it possible to do it using some
algorithm and function object from STL?

e.g.

std::vector<Base*> base_vector;
std::vector<Derived*> derived_vector;

// populate base vector..

I would like to say something like,

std::transform(base_vector.begin(), base_vector.end(),
std::back_inserter(derived_vector), dynamic_cast<Derived*>());

To be short, I am looking for a function object which can convert one
type of object to another. I think cast operators don't behave like
function objects so they can't be passed to stl algorithms.

Thanks
Ram
 
V

Victor Bazarov

Ram said:
I have a container of base pointers and I want to generate a container
of derived class pointers from it by casting each pointer of base class
to a pointer of derived class. Is it possible to do it using some
algorithm and function object from STL?

e.g.

std::vector<Base*> base_vector;
std::vector<Derived*> derived_vector;

// populate base vector..

I would like to say something like,

std::transform(base_vector.begin(), base_vector.end(),
std::back_inserter(derived_vector), dynamic_cast<Derived*>());

To be short, I am looking for a function object which can convert one
type of object to another. I think cast operators don't behave like
function objects so they can't be passed to stl algorithms.

Just roll your own:

template<class T> struct dynamic_caster {
template<class U> T operator()(U u) const
{ return dynamic_cast<T>(u); }
};

....
std::transform(...
, dynamic_caster<Derived*>());


V
 
P

Pete Becker

Victor said:
Just roll your own:

template<class T> struct dynamic_caster {
template<class U> T operator()(U u) const
{ return dynamic_cast<T>(u); }

return dynamic_cast<T>(*u);

The function call operator will be called with an iterator, and the code
needs to dereference it to get to the stored pointer.
 
V

Victor Bazarov

Pete said:
return dynamic_cast<T>(*u);

The function call operator will be called with an iterator, and the code
needs to dereference it to get to the stored pointer.

Absolutely. Thank you.
 
V

Victor Bazarov

Pete said:
return dynamic_cast<T>(*u);

The function call operator will be called with an iterator, and the code
needs to dereference it to get to the stored pointer.

Really? On an iterator? I thought it was specified as op(*(blah)), OW
dereferencing the iterator before calling the op...

V
 
P

Pete Becker

Victor said:
Really? On an iterator? I thought it was specified as op(*(blah)), OW
dereferencing the iterator before calling the op...

You're right. Ignore my correction.
 
F

Frank Chang

Ram, The class Base must have at least one virtual function for this to
compile.
 
R

Ram

To be short, I am looking for a function object which can convert one
Just roll your own:

template<class T> struct dynamic_caster {
template<class U> T operator()(U u) const
{ return dynamic_cast<T>(u); }
};

Thanks Victor, I have already done something similar. But as mentioned
in my OP, I was wondering if the standard library already provides some
function/functor for this.

Ram
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top