473,385 Members | 1,343 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,385 software developers and data experts.

STL list's assignment operator

Hi,

reading the output of gprof for one of my projects, I found that the
STL list assignment operator consumes a larger fraction of the
program's execution time. The exact entry in gprof's output looks
as follows:

std::list<MyClass*, std::allocator<MyClass*::operator=
(std::list<MyClass*, std::allocator<MyClass* const&) >

Since I don't know the source code for STL lists, I assume that
calling the assignment operator means that for each element
of the STL list the element's assignment operator is called. If
this is right, I don't understand why this works at all since I
explicitly disallow copying of MyClass objects by defining its
assignment operator private:

class MyClass:
private:
MyClass& operator=( const& MyClass );

So, the compiler does not create an default assignment operator
automatically. Thus, IMHO, STL list's operator= should fail.

Any ideas why this still works?

Regards,
Chris

Aug 6 '06 #1
3 2697
Christian Christmann wrote:
Hi,

reading the output of gprof for one of my projects, I found that the
STL list assignment operator consumes a larger fraction of the
program's execution time. The exact entry in gprof's output looks
as follows:

std::list<MyClass*, std::allocator<MyClass*::operator=
(std::list<MyClass*, std::allocator<MyClass* const&) >

Since I don't know the source code for STL lists, I assume that
calling the assignment operator means that for each element
of the STL list the element's assignment operator is called. If
this is right, I don't understand why this works at all since I
explicitly disallow copying of MyClass objects by defining its
assignment operator private:

class MyClass:
private:
MyClass& operator=( const& MyClass );

So, the compiler does not create an default assignment operator
automatically. Thus, IMHO, STL list's operator= should fail.

Any ideas why this still works?
Your list seems to hold MyClass _pointers_ so only the pointers are
assigned not the objects themselves.

Aug 6 '06 #2
On Sun, 06 Aug 2006 19:10:37 +0200, Christian Christmann
<pl*****@yahoo.dewrote:
>reading the output of gprof for one of my projects, I found that the
STL list assignment operator consumes a larger fraction of the
program's execution time. The exact entry in gprof's output looks
as follows:

std::list<MyClass*, std::allocator<MyClass*::operator=
(std::list<MyClass*, std::allocator<MyClass* const&) >

Since I don't know the source code for STL lists, I assume that
calling the assignment operator means that for each element
of the STL list the element's assignment operator is called.
... and the assignment operator also allocates a new node for each
element in the list (which probably is the performance problem). Try
vector instead (using reserve()).
>If
this is right, I don't understand why this works at all since I
explicitly disallow copying of MyClass objects by defining its
assignment operator private:

class MyClass:
private:
MyClass& operator=( const& MyClass );

So, the compiler does not create an default assignment operator
automatically. Thus, IMHO, STL list's operator= should fail.

Any ideas why this still works?
Your list contains pointers. MyClass objects are not copied, only
pointers to MyClass objects. After assignment the pointers in both
lists point to the same objects. This may or may not be what you want.
Note that STL containers use value semantics. They are made to work
with values, not with pointers to objects.

Best regards,
Roland Pibinger
Aug 6 '06 #3
In article <44***********************@newsread2.arcor-online.net>,
pl*****@yahoo.de says...
Hi,

reading the output of gprof for one of my projects, I found that the
STL list assignment operator consumes a larger fraction of the
program's execution time. The exact entry in gprof's output looks
as follows:

std::list<MyClass*, std::allocator<MyClass*::operator=
(std::list<MyClass*, std::allocator<MyClass* const&) >

Since I don't know the source code for STL lists, I assume that
calling the assignment operator means that for each element
of the STL list the element's assignment operator is called. If
this is right, I don't understand why this works at all since I
explicitly disallow copying of MyClass objects by defining its
assignment operator private:
One possibility would be that it's using a copy constructor to copy the
nodes.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 6 '06 #4

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

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
5
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set...
2
by: Barry Hynes | last post by:
G'Day folks, Have been working on this problem for quite some time and still no farther ahead. :( Here is my problem...bare with me i am very green :) I have to implement a Safe List,...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
6
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
3
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
6
by: Taras_96 | last post by:
Hi everyone, The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 states that: "Consider the following constructor that initializes member object x_ using an initialization...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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.