Question about copy constructors / distructors

E

ennio

Hi, i have a doubt i can't solve right now.
Your help will be appreciated.

I am studying constructors/copy constructors/destructors.
I created this a little class (see below).
If i comment the destructor (~Mine), i get the following lines as i
execute main:
"Constructor
Copy Constructor"
, as i expected (in the main function i initialize a variable).
But, if i uncomment the destructor, i can read
"Constructor"
only.

Can you tell me why or give me an hint?
Thank you very much

p.s. I am using VC6

----------------CUT-------------------
#include <iostream>
#include <conio.h>

using namespace std;

class Mine {
public:
char *s;
Mine(char *s) {
cout << "Constructor" << endl;
this->s = new char[strlen(s) + 1];
strcpy(this->s, s);
}
Mine(const Mine& object) {
cout << "Copy Constructor" << endl;
this->s = new char[strlen(object.s) + 1];
strcpy(this->s, object.s);
}
~Mine() {
cout << "Destructor" << endl;
delete [] s;
}
};

void main() {
Mine mine = Mine("Try1");
while(!kbhit());
}
----------------CUT-------------------
 
R

Rolf Magnus

ennio said:
Hi, i have a doubt i can't solve right now.
Your help will be appreciated.

I am studying constructors/copy constructors/destructors.
I created this a little class (see below).
If i comment the destructor (~Mine), i get the following lines as i
execute main:
"Constructor
Copy Constructor"
, as i expected (in the main function i initialize a variable).
But, if i uncomment the destructor, i can read
"Constructor"
only.

Can you tell me why or give me an hint?

Well, your main function basically first creates a nameless temporary Mine
object, then copy-construcs mine from it. However, the compiler is allowed
to optimize the temporary away, so that mine is directly initialized with
your string. It sounds strange that this would depend on whether you have a
user-defined destructor or not, but it would be permitted.
Thank you very much

p.s. I am using VC6

It would be good to upgrade that. It's very old and not really
standard-compliant.
----------------CUT-------------------
#include <iostream>
#include <conio.h>

using namespace std;

class Mine {
public:
char *s;
Mine(char *s) {

You should prefer the std::string class for strings. If you can't for some
reason, make that:

Mine(const char* s) {
cout << "Constructor" << endl;
this->s = new char[strlen(s) + 1];
strcpy(this->s, s);
}
Mine(const Mine& object) {
cout << "Copy Constructor" << endl;
this->s = new char[strlen(object.s) + 1];
strcpy(this->s, object.s);
}

Don't forget to add an operator=.
~Mine() {
cout << "Destructor" << endl;
delete [] s;
}
};

void main() {

main() must always return int.
 
V

Victor Bazarov

ennio said:
[..]
If i comment the destructor (~Mine), i get the following lines as i
execute main:
"Constructor
Copy Constructor"
, as i expected (in the main function i initialize a variable).
But, if i uncomment the destructor, i can read
"Constructor"
only.

Can you tell me why or give me an hint?

Apparently, the compiler produces different code based on the presence
of the destructor. It is allowed to.
Thank you very much

p.s. I am using VC6

That's very unfortunate. I strongly recommend you to upgrade to 2003
or 2005.
----------------CUT-------------------
#include <iostream>
#include <conio.h>

This is a non-standard header. It's customary to provide its contents
or not use it at all when posting here.
using namespace std;

class Mine {
public:
char *s;
Mine(char *s) {
cout << "Constructor" << endl;
this->s = new char[strlen(s) + 1];
strcpy(this->s, s);
}
Mine(const Mine& object) {
cout << "Copy Constructor" << endl;
this->s = new char[strlen(object.s) + 1];
strcpy(this->s, object.s);
}
~Mine() {
cout << "Destructor" << endl;
delete [] s;
}
};

void main() {

C++ does not have "void main". 'main' returns 'int'.
Mine mine = Mine("Try1");

The line above is called "copy-initialisation". The compiler is allowed
not to create the temporary, but instead initialise the object directly.
It is allowed, of course, not to optimise.
while(!kbhit());

There is no function 'kbhit' in C++. Try to avoid posting non-standard
code when asking questions in comp.lang.c++.
}
----------------CUT-------------------

V
 
M

mlimber

ennio said:
Hi, i have a doubt i can't solve right now.
Your help will be appreciated.

I am studying constructors/copy constructors/destructors.
I created this a little class (see below).
If i comment the destructor (~Mine), i get the following lines as i
execute main:
"Constructor
Copy Constructor"
, as i expected (in the main function i initialize a variable).
But, if i uncomment the destructor, i can read
"Constructor"
only.

Can you tell me why or give me an hint?
Thank you very much

p.s. I am using VC6

----------------CUT-------------------
#include <iostream>
#include <conio.h>

Non-standard header.
using namespace std;

class Mine {
public:
char *s;

Prefer std::string to managing memory on your own. See
http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1.
Mine(char *s) {
cout << "Constructor" << endl;
this->s = new char[strlen(s) + 1];
strcpy(this->s, s);
}
Mine(const Mine& object) {
cout << "Copy Constructor" << endl;
this->s = new char[strlen(object.s) + 1];
strcpy(this->s, object.s);
}

It's not necessary to qualify members with "this->".
~Mine() {
cout << "Destructor" << endl;
delete [] s;
}
};

void main() {

int main() { // http://parashift.com/c++-faq-lite/newbie.html#faq-29.3
Mine mine = Mine("Try1");
while(!kbhit());
}
----------------CUT-------------------

Are you sure? When I run it on VC6 (sp6) or g++ 3.4.4, I get:

Constructor
Destructor

Cheers! --M
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top