473,472 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Constructor / destructor call mismatches


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

Jul 19 '05 #1
1 1862
WW
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
Jul 19 '05 #2

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

Similar topics

7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
6
by: giles | last post by:
Hi, Does a compiler generated constructor initialise the data members? Giles
9
by: sahukar praveen | last post by:
Hello, This is the program that I am trying. The program executes but does not give me a desired output. ********************************************** #include <iostream.h> #include...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
3
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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...
0
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...
0
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 ...
0
muto222
php
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.