473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Question about copy constructors / distructors

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-------------------
Aug 16 '06 #1
3 1739
ennio wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 16 '06 #2
ennio wrote:
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.
Mine mine = Mine("Try1");
while(!kbhit());
}
Aug 16 '06 #3
ennio wrote:
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-lit....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

Aug 16 '06 #4

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
45
by: Robbie Hatley | last post by:
Hello, group. I've been doing too much C++ programming lately, and I'm starting to become rusty at some aspects of the C way of doing things, esp. efficient low-level data copies. ...
4
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
8
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.