473,695 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Slicing instances in STL containers

Hello,

I have problem that when I use std::list<MyCla ssand then store
various subclasses of MyClass in that list (or any other STL container)
the instances get sliced.

I have read FAQ: '[20.8] 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.

The solutions I can think about is:
a) Store pointers to MyClass in the container
- for this I have to write custom copy ctr's and operator=() for
all classes using std::list<MyCla ss>
b) Create wrapper class that "owns" my class
- this wrapper would call clone() to copy the owned instance
- this however one more level of indirection and I will have to
add proxy for almost all methods of MyClass to this wrapper (I make
heavy use of STL algorithms to work with the list

Is there any other "easy" solution to this problem, that I might miss?

Ales
Aug 28 '06 #1
19 3393
AlesD wrote:
Hello,

I have problem that when I use std::list<MyCla ssand then store
various subclasses of MyClass in that list (or any other STL container)
the instances get sliced.

I have read FAQ: '[20.8] 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.

The solutions I can think about is:
a) Store pointers to MyClass in the container
- for this I have to write custom copy ctr's and operator=() for
all classes using std::list<MyCla ss>
b) Create wrapper class that "owns" my class
- this wrapper would call clone() to copy the owned instance
- this however one more level of indirection and I will have to
add proxy for almost all methods of MyClass to this wrapper (I make
heavy use of STL algorithms to work with the list

Is there any other "easy" solution to this problem, that I might miss?
Pointers it is, however, you can make your life easier using smart pointers.
If you want to keep copy semantics, you should use a cloning smart pointer
or a copy smart pointer. Google this group for those terms and you will
find code. If you are happy with reference count base reference semantics,
you can use tr1::shared_ptr . In either case, the compiler provided copy
constructors and assignment operators for classes that contain a

std::my_list< well_chosen_sma rt_pointer< MyClass

will work fine.
Best

Kai-Uwe Bux

Aug 28 '06 #2
On Mon, 28 Aug 2006 07:36:13 -0400, Kai-Uwe Bux <jk********@gmx .net>
wrote:
>Pointers it is, however, you can make your life easier using smart pointers.
If you want to keep copy semantics, you should use a cloning smart pointer
or a copy smart pointer. Google this group for those terms and you will
find code. If you are happy with reference count base reference semantics,
you can use tr1::shared_ptr . In either case, the compiler provided copy
constructors and assignment operators for classes that contain a

std::my_list< well_chosen_sma rt_pointer< MyClass

will work fine.
Can you explain what will work fine? Sure, tr1::shared_ptr gives you
object lifetime management, albeit the most inefficient known so far
(one dynamically allocted counter for each pointed-to object).
Otherwise, tr1::shared_ptr imitates real pointers and therefore
'inherits' the mismatch between STL value semantics and pointers. So,
why put "smart" pointers into STL containers at all? It makes no
sense.

Best wishes,
Roland Pibinger
Aug 28 '06 #3
Roland Pibinger wrote:
>will work fine.
[anti-pointer-in-container rant redacted]
OK, Roland. I have the following requirements: I need a container of
polymorphic objects. For various reasons, on this project, I am not
allowed to use third party libraries (don't suggest Rogue Wave).

How do I do it?

Aug 28 '06 #4
Roland Pibinger wrote:
Can you explain what will work fine? Sure, tr1::shared_ptr gives you
object lifetime management, albeit the most inefficient known so far
(one dynamically allocted counter for each pointed-to object).
Boost's implementation can be configured to use a memory pool, so it
doesn't dynamically allocate a pointer per pointed-to object.

But admittedly, I don't know any other decent smart pointer
implementation. Can you point me to one or more?

Jens
Aug 28 '06 #5
Roland Pibinger wrote:
On Mon, 28 Aug 2006 07:36:13 -0400, Kai-Uwe Bux <jk********@gmx .net>
wrote:
Pointers it is, however, you can make your life easier using smart pointers.
If you want to keep copy semantics, you should use a cloning smart pointer
or a copy smart pointer. Google this group for those terms and you will
find code. If you are happy with reference count base reference semantics,
you can use tr1::shared_ptr . In either case, the compiler provided copy
constructors and assignment operators for classes that contain a

std::my_list< well_chosen_sma rt_pointer< MyClass

will work fine.

Can you explain what will work fine? Sure, tr1::shared_ptr gives you
object lifetime management, albeit the most inefficient known so far
(one dynamically allocted counter for each pointed-to object).
Otherwise, tr1::shared_ptr imitates real pointers and therefore
'inherits' the mismatch between STL value semantics and pointers. So,
why put "smart" pointers into STL containers at all? It makes no
sense.
Could you please explain what you mean by mismatch, and--if
possible--provide an alternate solution that avoids it?

Aug 28 '06 #6
On Mon, 28 Aug 2006 17:26:03 GMT, red floyd <no*****@here.d udewrote:
>Roland Pibinger wrote:
>will work fine.
[anti-pointer-in-container rant redacted]
Which rant? I've asked a simple question and now I get only counter
questions instead of an answer.
>OK, Roland. I have the following requirements: I need a container of
polymorphic objects.
Not an uncommon requirement.
>For various reasons, on this project, I am not
allowed to use third party libraries (don't suggest Rogue Wave).
I'm not a Rogue Wave salesperson ;-)
>How do I do it?
Someone with your skill set can write a container for pointers (real
pointers, of course, not "smart" pointers).

Best wishes,
Roland Pibinger
Aug 28 '06 #7
On Mon, 28 Aug 2006 19:20:11 +0100, Jens Theisen <jt***@arcor.de >
wrote:
>Boost's implementation can be configured to use a memory pool, so it
doesn't dynamically allocate a pointer per pointed-to object.
What is the 'heap' or 'free store' but a memory pool?
>But admittedly, I don't know any other decent smart pointer
implementation . Can you point me to one or more?
You don't need any smart pointer. You need
containers/iterators/algorithms that work appropriately with pointers.

Best wishes,
Roland Pibinger
Aug 28 '06 #8
On 28 Aug 2006 12:22:23 -0700, "Squeamizh" <sq*****@hotmai l.com>
wrote:
>Could you please explain what you mean by mismatch, and--if
possible--provide an alternate solution that avoids it?
std::vector<MyC lass*myVec;
std::sort (myVec.begin(), myVec.end());

In the above example the sort function template does the wrong thing:
it sorts pointers (addresses). That's the mismatch. It should sort
according to the pointed-to objects but exchange only pointers. The
reason for the strange behavior is that STL was deliberately made
without any awareness for pointers (a.k.a. 'value semantics'). The
'usual' workaround is to provide a user-defined compare object as
third argument - a workaround that confirms the mismatch.

Best wishes,
Roland Pibinger
Aug 28 '06 #9
Roland Pibinger wrote:
What is the 'heap' or 'free store' but a memory pool?
shared_ptrs memory pool is a special purpose memory pool which can be
faster since it knows the size of the chunk it's going to allocate in
each step. I don't know how much it gains though.

I agree that it would be beneficial to have an intrusive smart pointer
as an alternative.
You don't need any smart pointer. You need
containers/iterators/algorithms that work appropriately with pointers.
Uh? I'm not sure if I understand exactly what you mean.

The only decent ways of memory management I did come accross so far have
been smart pointers, pools (as in APR), and sophisticated garbage
collection.

Jens
Aug 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3235
by: hokiegal99 | last post by:
How would I determine if a filename is greater than a certain number of characters and then truncate it to that number? For example a file named XXXXXXXXX.txt would become XXXXXX fname = files if fname > 6 print fname Thanks!!!
9
2470
by: Jerry Sievers | last post by:
Fellow Pythonists; I am totally puzzled on the use of slicing on mapping types and especially unsure on use of the Ellipsis... and slicing syntax that has two or more groups seperated by comma. I am referring to (from the manual); Slice objects Slice objects are used to represent slices when extended
3
2054
by: Bryan Olson | last post by:
I recently wrote a module supporting value-shared slicing. I don't know if this functionality already existed somewhere, but I think it's useful enough that other Pythoners might want it, so here it is. Also, my recent notes on Python warts with respect to negative indexes were based on problems I encoutered debugging this module, so I'm posting it partially as a concrete example of what I was talking about.
11
2039
by: jbperez808 | last post by:
>>> rs='AUGCUAGACGUGGAGUAG' >>> rs='GAG' Traceback (most recent call last): File "<pyshell#119>", line 1, in ? rs='GAG' TypeError: object doesn't support slice assignment You can't assign to a section of a sliced string in Python 2.3 and there doesn't seem to be mention of this as a Python 2.4 feature (don't have time to actually try
17
16550
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee part of Manager is copied. ".... and gives the code above as .....
17
3566
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit B(INT32 i =0):i_(i){} virtual ~B(){}
1
3469
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } // A virtual destructor
2
4702
by: Rahul | last post by:
Hi Everyone, I was working around object slicing (pass by value) and was wondering how it is specified in the standard, class A { }; class B: public A
1
2789
by: subramanian100in | last post by:
Consider class Base { .... }; class Derived : public Base { ...
0
8630
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...
1
8845
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
8825
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
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4340
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
4579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3003
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2272
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.