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:
"Constructo r
Copy Constructor"
, as i expected (in the main function i initialize a variable).
But, if i uncomment the destructor, i can read
"Constructo r"
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 << "Constructo r" << 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------------------- 3 1762
ennio wrote:
[..]
If i comment the destructor (~Mine), i get the following lines as i
execute main:
"Constructo r
Copy Constructor"
, as i expected (in the main function i initialize a variable).
But, if i uncomment the destructor, i can read
"Constructo r"
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 << "Constructo r" << 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
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:
"Constructo r
Copy Constructor"
, as i expected (in the main function i initialize a variable).
But, if i uncomment the destructor, i can read
"Constructo r"
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 << "Constructo r" << 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()) ;
}
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:
"Constructo r
Copy Constructor"
, as i expected (in the main function i initialize a variable).
But, if i uncomment the destructor, i can read
"Constructo r"
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 << "Constructo r" << 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn't
mean that they couldn't be copied from another object of the same kind when...
|
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.
Specificially, I just wrote the following, but I don't know if this
is safe:
void Func(char* left, char* right)
{
chat temp_left = {'\0'};
|
by: Shraddha |
last post by:
What is the use of "PURE vitual distructors"?
And why we can not have vitual constructors?
|
by: Shraddha |
last post by:
What is the use of "PURE vitual distructors"?
And why we can not have vitual constructors?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |