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

three times copy ctor called, one ctor called, why?

#include <iostream>
#include <string>
#include <map>
using namespace std ;
class tst
{
public :
tst() { cout << "tst::constructor" << endl ; }
tst(const tst & that) { cout << "tst::copy constructor" << endl ;
}
tst & operator =( const tst & that ) { cout << "tst::operator ="
<< endl ; }

~tst() { cout << "tst::destructor" << endl ; }
} ;
int main()
{
map < string , tst > a ;
tst e ;
a.insert(pair < string , tst > ("standard",e) ) ;

system("pause") ;
return 0 ;
}
///// Output ////////////
tst::constructor
tst::copy constructor
tst::copy constructor
tst::copy constructor
tst::destructor
tst::destructor
Jul 22 '05 #1
4 1407
Apricot wrote:
#include <iostream>
#include <string>
#include <map>
using namespace std ;
class tst
{
public :
tst() { cout << "tst::constructor" << endl ; }
tst(const tst & that) { cout << "tst::copy constructor" << endl ;
}
tst & operator =( const tst & that ) { cout << "tst::operator ="
<< endl ; }
Why have you not returned anything from this function?

~tst() { cout << "tst::destructor" << endl ; }
} ;
int main()
{
map < string , tst > a ;
tst e ;
a.insert(pair < string , tst > ("standard",e) ) ;
Actually, that should be 'const string'. I think it should be OK this
way, but I seem to recall some implementations having trouble inserting
into a map this way, or by using make_pair. For that reason, using
map<>::value_type (which you can be sure has exactly the right type) may
be preferable. I'd probably do this:

typedef map<string, tst> MapType;
MapType a;
tst e;
a.insert(MapType::value_type("standard", e));

system("pause") ;
return 0 ;
}
///// Output ////////////
tst::constructor
tst::copy constructor
tst::copy constructor
tst::copy constructor
tst::destructor
tst::destructor


I don't see these results. What I do see might shed some light, however:

tst::constructor
tst::copy constructor
tst::copy constructor
tst::copy constructor
tst::destructor
tst::destructor
pause: not found
tst::destructor
tst::destructor

Notice that the ill-fated "pause" command came before two of the
destructor calls. Perhaps your pausing is preventing you from seeing
some of the destructor calls. If not that, then I suspect your compiler
is broken and due for an update.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #2
You are expecting a single copy ctor, because e is passed to a.insert.
but it should be called one more time - ("standard", e) to pair <T1,
T2> conversion. now why it is being called third time probably is due
to internal pair implementation (object is copied somewhere in temp
object) ?

Amit Manocha

Apricot <yi**@21cn.com> wrote in message news:<cb********************************@4ax.com>. ..
#include <iostream>
#include <string>
#include <map>
using namespace std ;
class tst
{
public :
tst() { cout << "tst::constructor" << endl ; }
tst(const tst & that) { cout << "tst::copy constructor" << endl ;
}
tst & operator =( const tst & that ) { cout << "tst::operator ="
<< endl ; }

~tst() { cout << "tst::destructor" << endl ; }
} ;
int main()
{
map < string , tst > a ;
tst e ;
a.insert(pair < string , tst > ("standard",e) ) ;

system("pause") ;
return 0 ;
}
///// Output ////////////
tst::constructor
tst::copy constructor
tst::copy constructor
tst::copy constructor
tst::destructor
tst::destructor

Jul 22 '05 #3
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<TW******************@newsread2.news.pas.eart hlink.net>...
Apricot wrote:
#include <iostream>
#include <string>
#include <map>
using namespace std ;
class tst
{
public :
tst() { cout << "tst::constructor" << endl ; }
tst(const tst & that) { cout << "tst::copy constructor" << endl ;
}
tst & operator =( const tst & that ) { cout << "tst::operator ="
<< endl ; }

~tst() { cout << "tst::destructor" << endl ; }
} ;
int main()
{
map < string , tst > a ;
tst e ;
a.insert(pair < string , tst > ("standard",e) ) ;

system("pause") ;
return 0 ;
}
///// Output ////////////
tst::constructor
tst::copy constructor
tst::copy constructor
tst::copy constructor
tst::destructor
tst::destructor


I don't see these results. What I do see might shed some light, however:

tst::constructor
tst::copy constructor
tst::copy constructor
tst::copy constructor
tst::destructor
tst::destructor
pause: not found
tst::destructor
tst::destructor

Notice that the ill-fated "pause" command came before two of the
destructor calls. Perhaps your pausing is preventing you from seeing
some of the destructor calls. If not that, then I suspect your compiler
is broken and due for an update.


Sorry for the long quote, but isn't this obvious? e is destroyed after
the pause, and the single tst in a is also destroyed later.

Regards,
Michiel Salters
Jul 22 '05 #4
Apricot <yi**@21cn.com> wrote in message news:<cb********************************@4ax.com>. ..
#include <iostream>
#include <string>
#include <map>
using namespace std ;
class tst
{
public :
tst() { cout << "tst::constructor" << endl ; }
tst(const tst & that) { cout << "tst::copy constructor" << endl ;
}
tst & operator =( const tst & that ) { cout << "tst::operator ="
<< endl ; }

~tst() { cout << "tst::destructor" << endl ; }
} ;
int main()
{
map < string , tst > a ;
tst e ;
a.insert(pair < string , tst > ("standard",e) ) ;

system("pause") ;
return 0 ;
}
///// Output //////////// tst::constructor
tst::copy constructor
tst::copy constructor
tst::copy constructor
tst::destructor
tst::destructor


From what I understand all the STL containers get populated by
copying. This means that the only time the constructor gets called is
when you do
tst e;
a.insert(pair < string , tst > ("standard",e) );
First you create a (temporary) pair which gets populated by copying
("standard", e) into it.

In the second step the internal workings of the map implementation you
seem to be using creates a pair for internal use (2nd temporary in
this case) by copying ("standard", e) from the first pair into it.

Finally at the third step the map creates a node in the binary tree
that is populated by copying the 2nd temporary pair into it. (Note: it
is not required that any implementation uses a binary tree to store
map data, it just happens that all the implementations I know do it
that way)

fourth step is destroying the 2nd temporary pair (as seen by your
tst::destructor output), the one created by the insert action

fifth step is destroying the first temporary pair, the one you
explicitly created.
Hopefully the next bit of code can make it a bit clearer. replace your
main with it (it should also give you two more destructor calls).
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. {
  4. map < string , tst > a;
  5. cout << "map created\n" ;
  6. tst e ;
  7. cout << "tst e created\n" ;
  8. std::pair<string, tst> mep("standard", e);
  9. cout << "pair created\n";
  10. a.insert(mep) ;
  11. }
  12.  
  13. cout << "mash keyboard to continue" << endl;
  14. cin.get();
  15. return 0 ;
  16. }
  17.  
Jul 22 '05 #5

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

Similar topics

9
by: djskrill | last post by:
Why is the Constructor called 4 times but the Destructor 5 times? I am using MS VC 6. If this has been covered already please let me know. The Code: #include <stdio.h> class MyClass {...
39
by: scooter | last post by:
Given this class heirarchy class Base{}; class A : public Base {}; class B : public Base {};
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
3
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
15
by: subramanian100in | last post by:
consider the following program: #include <iostream> using namespace std; class Test { public: Test(int xx) : x(xx) { cout << x << endl; }
7
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.