Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Constructor / destructor call mismatches

Reply
Thread Tools

Constructor / destructor call mismatches

 
 
Dave Theese
Guest
Posts: n/a
 
      09-24-2003

Hello all,

Please see the two questions and output embedded in the program below.

Thank you!
Dave


#ifdef WIN32
#pragma warning(disable: 4786)
#endif

#include <iostream>
#include <map>

using namespace std;

template <int type_differentiator>
class foo
{
public:
explicit foo(int d): data(d)
{
cout << "foo<"
<< type_differentiator
<< ">() : "
<< data
<< endl;
}

~foo()
{
cout << "~foo<"
<< type_differentiator
<< ">(): "
<< data
<< endl;
}

bool operator<(const foo &lhs) const {return data < lhs.data;}

private:
int data;
};

int main()
{
map<foo<1>, foo<2> > bar;

cout << "Starting..." << endl;

// Question 1:
// How does the line below compile (when not commented out)? foo's
// constructor is explicit!
// bar.insert(make_pair(42, 17));

// Question 2:
// This line leads to an uneven number of constructor and destructor
calls.
// How is this possible?
bar.insert(
make_pair(
foo<1>(42),
foo<2>(17)
)
);

cout << "Exiting..." << endl;

return 0;
}

// As shown, the program above generates the output below when built with
// VC++ 6.0:
//
// Starting...
// foo<2>() : 17
// foo<1>() : 42
// ~foo<2>(): 17
// ~foo<1>(): 42
// ~foo<2>(): 17
// ~foo<1>(): 42
// ~foo<1>(): 42
// ~foo<2>(): 17
// Exiting...
// ~foo<2>(): 17
// ~foo<1>(): 42

// As shown, the program above generates the output below when built with
g++:
//
// Starting...
// foo<1>() : 42
// foo<2>() : 17
// ~foo<2>(): 17
// ~foo<1>(): 42
// ~foo<2>(): 17
// ~foo<1>(): 42
// ~foo<2>(): 17
// ~foo<1>(): 42
// Exiting...
// ~foo<2>(): 17
// ~foo<1>(): 42



 
Reply With Quote
 
 
 
 
WW
Guest
Posts: n/a
 
      09-24-2003
Dave Theese wrote:
> Hello all,
>
> Please see the two questions and output embedded in the program below.

[SNIP]
> template <int type_differentiator>
> class foo
> {
> public:
> explicit foo(int d): data(d)
> {
> cout << "foo<"
> << type_differentiator
> << ">() : "
> << data
> << endl;
> }
>
> ~foo()
> {
> cout << "~foo<"
> << type_differentiator
> << ">(): "
> << data
> << endl;
> }
>
> bool operator<(const foo &lhs) const {return data < lhs.data;}
>
> private:
> int data;
> };


You are missing the copy constructor. Copies never show up as constructed
but they will show up as destructed.

--
WW aka Attila


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting sub/call mismatches? Tim McDaniel Perl Misc 3 11-21-2012 04:40 PM
Search for a string in another string allowing mismatches Janus Bor Ruby 3 09-26-2010 08:33 AM
Superclass mismatches listrecv@gmail.com Ruby 3 08-07-2006 09:21 AM
Call constructor/destructor of base class Fabian Müller C++ 23 07-11-2004 03:20 AM
prototype mismatches Gary C++ 4 01-07-2004 08:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57