471,612 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,612 software developers and data experts.

Converting containers of smart pointers

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);
}
Jul 22 '05 #1
4 1254
Eric wrote:
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?

return (0);
}


Jul 22 '05 #2
Rolf Magnus <ra******@t-online.de> wrote in message news:<c0*************@news.t-online.com>...
Eric wrote:

// 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?


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.
Jul 22 '05 #3


Eric wrote:

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.

Jul 22 '05 #4
On 17 Feb 2004 21:36:45 -0800, er**@lemings.com (Eric) wrote:
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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Khalid | last post: by
5 posts views Thread by Matthias Kaeppler | last post: by
4 posts views Thread by Vincenzo Cappello | last post: by
19 posts views Thread by AlesD | last post: by
15 posts views Thread by Nindi73 | last post: by
21 posts views Thread by George Exarchakos | last post: by
1 post views Thread by XIAOLAOHU | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.