473,804 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

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

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

~tst() { cout << "tst::destructo r" << 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_ty pe (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(MapTyp e::value_type(" standard", e));

system("pause") ;
return 0 ;
}
///// Output ////////////
tst::constructo r
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::constructo r
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.co m> wrote in message news:<cb******* *************** **********@4ax. com>...
#include <iostream>
#include <string>
#include <map>
using namespace std ;
class tst
{
public :
tst() { cout << "tst::construct or" << endl ; }
tst(const tst & that) { cout << "tst::copy constructor" << endl ;
}
tst & operator =( const tst & that ) { cout << "tst::opera tor ="
<< endl ; }

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

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

Jul 22 '05 #3
Kevin Goodsell <us************ *********@never box.com> wrote in message news:<TW******* ***********@new sread2.news.pas .earthlink.net> ...
Apricot wrote:
#include <iostream>
#include <string>
#include <map>
using namespace std ;
class tst
{
public :
tst() { cout << "tst::construct or" << endl ; }
tst(const tst & that) { cout << "tst::copy constructor" << endl ;
}
tst & operator =( const tst & that ) { cout << "tst::opera tor ="
<< endl ; }

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

system("pause") ;
return 0 ;
}
///// Output ////////////
tst::constructo r
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::constructo r
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.co m> wrote in message news:<cb******* *************** **********@4ax. com>...
#include <iostream>
#include <string>
#include <map>
using namespace std ;
class tst
{
public :
tst() { cout << "tst::construct or" << endl ; }
tst(const tst & that) { cout << "tst::copy constructor" << endl ;
}
tst & operator =( const tst & that ) { cout << "tst::opera tor ="
<< endl ; }

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

system("pause") ;
return 0 ;
}
///// Output //////////// tst::constructo r
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
1719
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 { private: int x;
39
2895
by: scooter | last post by:
Given this class heirarchy class Base{}; class A : public Base {}; class B : public Base {};
10
2639
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 section 4.2. The copy constructor is defined as: auto_ptr (auto_ptr& rhs) throw() : ap (rhs. release()) { }
3
3406
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 demonstrates(?) that the copy constructor is never called (at least no sign of its chatter on std::cout is visible). So - is it necessary to have a public copy constructor in order to use "function style" initializers for an array of objects? ...
10
4032
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and destruction of ~Uncopyable() {} // derived objects... private: Uncopyable(const Uncopyable&); // ...but prevent copying
13
2483
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 certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
15
1839
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
163
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test { public: Test() { cout<<"Cons\n";} Test(Test& a) { cout<<"Copy cons\n";}
11
2294
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
0
9715
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
9595
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,...
1
10354
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
9175
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
7642
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
6867
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
5535
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...
1
4313
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
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.