destructor called twice

B

Barry

Hi, I have the following simple code -

#include <iostream>
#include "Event.h"

int main()
{
{
Event event;
std::pair<double,Event>(0.1,event);
}
std::cin.get();
return 0;
}

#ifndef EVENT_H
#define EVENT_H

#include <iostream>

class Event
{
public:
Event(void);
virtual ~Event(void);
};

#endif

#include "Event.h"

Event::Event()
{
std::cout << "Event()" << std::endl;
}

Event::~Event()
{
std::cout << "~Event()" << std::endl;
}


Why is the output as follows -

Event()
~Event()
~Event()

Since pair takes a reference to my event object, I don't see how there
can be a second object to destroy. Can anyone explain please?

Thanks,

Barry
 
A

Alexander Bartolich

Barry said:
{
Event event;
std::pair<double,Event>(0.1,event);
}
[...]
Since pair takes a reference to my event object,

std::pair<double,Event> is not the same as std::pair<double,Event&>.
 
B

Bo Persson

Barry said:
Hi, I have the following simple code -

#include <iostream>
#include "Event.h"

int main()
{
{
Event event;
std::pair<double,Event>(0.1,event);
}
std::cin.get();
return 0;
}

#ifndef EVENT_H
#define EVENT_H

#include <iostream>

class Event
{
public:
Event(void);
virtual ~Event(void);
};

#endif

#include "Event.h"

Event::Event()
{
std::cout << "Event()" << std::endl;
}

Event::~Event()
{
std::cout << "~Event()" << std::endl;
}


Why is the output as follows -

Event()
~Event()
~Event()

Since pair takes a reference to my event object, I don't see how
there can be a second object to destroy. Can anyone explain please?

The parameter might be a reference, but a std::pair<double,Event>
stores one double and one Event, making copies as needed.


Bo Persson
 
B

Barry

The parameter might be a reference, but a std::pair<double,Event>
stores one double and one Event, making copies as needed.

Bo Persson

Now I understand. And the implicit copy constructor is also at play
here.
 
J

Juha Nieminen

Barry said:
Event::Event()
{
std::cout << "Event()" << std::endl;
}

Event::~Event()
{
std::cout << "~Event()" << std::endl;
}

Try making the copy constructor print something as well.
 
B

Balog Pal

Barry said:
class Event
{
public:
Event(void);
virtual ~Event(void);
};

#endif

#include "Event.h"

Event::Event()
{
std::cout << "Event()" << std::endl;
}

This uses the computer-generated copy ctor. That just makes a copy without
being visible outside -- and objects created by it certainly will get
destructed by your noisy dtor.

Try one of the following additions:
class Event
{
public:
Event(void);
virtual ~Event(void);
Event(const Event&);
{ std::cout << "Event(const Event&)" << std::endl; }
};
or

class Event
{
public:
Event(void);
virtual ~Event(void);
private:
Event(const Event&); // left unimplemented

and I guess you'll get the mistery solved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,441
Members
47,101
Latest member
DoloresHol

Latest Threads

Top