473,792 Members | 3,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_p tr<A> AP;
typedef std::set<AP> AS;

typedef boost::shared_p tr<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 1362
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_p tr<A> AP;
typedef std::set<AP> AS;

typedef boost::shared_p tr<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_p tr<A> AP;
typedef boost::shared_p tr<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.co m (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_p tr<A> AP;
typedef std::set<AP> AS;

typedef boost::shared_p tr<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
4238
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? thanks a lot kh
3
2208
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 POD, because it has std::string and other classes requiring proper destruction, although all drill down to POD and strings eventually). Classic code layout:
9
1950
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> #include <iostream>
5
2575
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 the objects directly in a container, because they must not be duplicated, so I have to use pointers. I'm just not certain about using raw pointers or some kind of smart pointer. Regards,
44
3883
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 specialized for anything. Thanks, Josh McFarlane
4
1572
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, std::auto_ptr< base_class > > someone tell me is no correct using auto_ptr in containers...
19
3407
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 is a "virtual constructor"?', but I miss information if there is some "workaround" when using STL containers, since I can't change them to call clone() instead of copy constructor.
15
4099
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 containers destructors virtual >
21
2217
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 necessarily the same... class base { int x;
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10428
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.