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-------------------
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-------------------