Converting containers of smart pointers

E

Eric

See question in main function below...TIA.

struct A {};
struct B: public A {};

#include <boost/shared_ptr.hpp>
#include <set>

typedef boost::shared_ptr<A> AP;
typedef std::set<AP> AS;

typedef boost::shared_ptr<B> BP;
typedef std::set<BP> BS;

void F (AS& as) {
//...do some processing on as
}

int main (void) {
BS bs;
//...fill as with some pointers...

// How do you pass bs to F?

return (0);
}
 
R

Rolf Magnus

Eric said:
See question in main function below...TIA.

struct A {};
struct B: public A {};

#include <boost/shared_ptr.hpp>
#include <set>

typedef boost::shared_ptr<A> AP;
typedef std::set<AP> AS;

typedef boost::shared_ptr<B> BP;
typedef std::set<BP> BS;

void F (AS& as) {
//...do some processing on as
}

int main (void) {
BS bs;
//...fill as with some pointers...

// How do you pass bs to F?

You can't. BS is not derived from AS. The two are distinct types. Why
not simply fill an AS with the pointers and pass that one?
 
E

Eric

Rolf Magnus said:
You can't. BS is not derived from AS. The two are distinct types. Why
not simply fill an AS with the pointers and pass that one?

Well I suppose that's one solution.

Here's another question though. Forget the containers. Is a smart pointer
to a derived class convertible to a smart pointer to a base class? (I don't
have access to a suitable DE at the moment or I'd test it myself.) Example:

struct A {};
struct B: public A {};

#include <boost/shared_ptr.hpp>

typedef boost::shared_ptr<A> AP;
typedef boost::shared_ptr<B> BP;

void F (AP ap) {
//...do some processing on ap
}

int main (void) {
BP bp (new B);
// Can you pass bp to F?
return (0);
}

If so, then maybe you can use a set copy constructor to create a set of base
smart pointers from a set of derived smart pointers. Possible?

Cheers,
Eric.
 
L

lilburne

Eric said:
Here's another question though. Forget the containers. Is a smart pointer
to a derived class convertible to a smart pointer to a base class? (I don't
have access to a suitable DE at the moment or I'd test it myself.) Example:

I would hope not. The only possibility would be if you could create a
temporary AP from the BP, which would mean require an AP from the
pointer held in BP; but constructing a smart pointer gives ownership of
the pointer to the smart pointer; you would then have two smart pointers
constructed independently from the same pointer. When the temporary AP
dies it will delete the pointer, and any further access via BP,
including BPs destruction will result in undefined behaviour.
 
T

tom_usenet

See question in main function below...TIA.

struct A {};
struct B: public A {};

#include <boost/shared_ptr.hpp>
#include <set>

typedef boost::shared_ptr<A> AP;
typedef std::set<AP> AS;

typedef boost::shared_ptr<B> BP;
typedef std::set<BP> BS;

void F (AS& as) {
//...do some processing on as
}

int main (void) {
BS bs;
//...fill as with some pointers...

// How do you pass bs to F?

You can't - it would be a violation of LSP, since F could replace the
held BPs with APs without a compiler error, and you'd end up with a BS
containing APs! If you pass the parameter by const reference, then
there's no problem:

F(AS(bs.begin(), bs.end()));

should do it (although it isn't very efficient). Better might be to
define F as a template. e.g.

template <class Cont>
void F(Cont& as)
{
//assume as elements are convertible to AP.
}

or an algorithm:

template <class FwdIt>
void F(FwdIt begin, FwdIt end)
{
//*begin convertible to AP.
}

It depends on what you are trying to do.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top