473,372 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 1337
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Khalid | last post by:
II am allocating alot of memory for my problem model which uses stl containers for these pointers, will stl free the memory? by other words what is the semantics of memory ownership in stl? ...
3
by: JackC | last post by:
Hi Problem: I wish to use a pimpl to hide implementation/storage of a class (T), but I also want to hold objects of that class (T) in an std::vector<T> (or similar). T is non trivial (i.e. not...
9
by: deancoo | last post by:
Hello all, I'm new to the C++ STL, and am trying to wrap my head around why the following piece of code doesn't compile. What am I doing wrong? Thanks for any help. d #include <cstdlib>...
5
by: Matthias Kaeppler | last post by:
Hi, I was wondering, since STL containers are based around copying, whether it's a good idea to use reference counted smart pointers, such as boost::shared_ptr in STL containers. I can't store...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
4
by: Vincenzo Cappello | last post by:
I need a map that contain different object of the same base class like: std::map< int, base_class* > someone tell me is no correct using pointer in containers so i change to: std::map< int,...
19
by: AlesD | last post by:
Hello, I have problem that when I use std::list<MyClassand then store various subclasses of MyClass in that list (or any other STL container) the instances get sliced. I have read FAQ: ' What...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.