473,386 Members | 2,050 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.

D-tor apparentlly isn't called here

#include <iostream>
#include <cstdlib>
#include <cmath>
#include "SmrtPtr.hpp"
using namespace std;

struct Data
{
double plus;
double mult;
double pow;
};

SmrtPtr<Datamath(const double& val,const double& val2);
int main()
{
for(;;)
{
cout << "Enter two values: " << endl;
double val, val2;
cin >val >val2;
SmrtPtr<Datatuple=math(val,val2);
cout << tuple->plus << endl << tuple->mult << endl << tuple->pow <<
endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

SmrtPtr<Datamath(const double& val,const double& val2)
{
SmrtPtr<Datatuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}

Here's the code for the class' d-tor:

~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase; cout << "Deleted." << endl;}
else {delete DataBase; cout << "Out of scope. " << endl;}
}
}

Jul 15 '06 #1
8 1311
"Protoman" <Pr**********@gmail.comwrote in message news:11*********************@i42g2000cwa.googlegro ups.com...
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "SmrtPtr.hpp"
using namespace std;

struct Data
{
double plus;
double mult;
double pow;
};

SmrtPtr<Datamath(const double& val,const double& val2);
int main()
{
for(;;)
{
cout << "Enter two values: " << endl;
double val, val2;
cin >val >val2;
SmrtPtr<Datatuple=math(val,val2);
cout << tuple->plus << endl << tuple->mult << endl << tuple->pow <<
endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

SmrtPtr<Datamath(const double& val,const double& val2)
{
SmrtPtr<Datatuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}
As I read that, it's returning a copy of a SmrtPtr object,
so a temporary is made. Since "tuple" is automatic storage
class, the original "tuple" should be destructed when math()
returns. The temporary copy is then returned to the calling
function.

Are you saying this is NOT what's happening when you run it?
Here's the code for the class' d-tor:

~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase; cout << "Deleted." << endl;}
else {delete DataBase; cout << "Out of scope. " << endl;}
}
}
What does it print when you run it?

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 15 '06 #2

Robbie Hatley wrote:
"Protoman" <Pr**********@gmail.comwrote in message news:11*********************@i42g2000cwa.googlegro ups.com...
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "SmrtPtr.hpp"
using namespace std;

struct Data
{
double plus;
double mult;
double pow;
};

SmrtPtr<Datamath(const double& val,const double& val2);
int main()
{
for(;;)
{
cout << "Enter two values: " << endl;
double val, val2;
cin >val >val2;
SmrtPtr<Datatuple=math(val,val2);
cout << tuple->plus << endl << tuple->mult << endl << tuple->pow <<
endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

SmrtPtr<Datamath(const double& val,const double& val2)
{
SmrtPtr<Datatuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}

As I read that, it's returning a copy of a SmrtPtr object,
so a temporary is made. Since "tuple" is automatic storage
class, the original "tuple" should be destructed when math()
returns. The temporary copy is then returned to the calling
function.

Are you saying this is NOT what's happening when you run it?
Here's the code for the class' d-tor:

~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase; cout << "Deleted." << endl;}
else {delete DataBase; cout << "Out of scope. " << endl;}
}
}

What does it print when you run it?

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
OK, here's the output:

Enter two values:
2 2
4
4
4
Deleted.
Press any key to continue...

Somewhere, it should be printing "Out of scope."

Jul 15 '06 #3
"Protoman" wrote:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "SmrtPtr.hpp"
using namespace std;
>
struct Data
{
double plus;
double mult;
double pow;
};
>
SmrtPtr<Datamath(const double& val,const double& val2);
int main()
{
for(;;)
{
cout << "Enter two values: " << endl;
double val, val2;
cin >val >val2;
SmrtPtr<Datatuple=math(val,val2);
cout << tuple->plus << endl << tuple->mult << endl << tuple->pow <<
endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
>
SmrtPtr<Datamath(const double& val,const double& val2)
{
SmrtPtr<Datatuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}
~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase; cout << "Deleted." << endl;}
else {delete DataBase; cout << "Out of scope. " << endl;}
}
}
What does it print when you run it?

OK, here's the output:

Enter two values:
2 2
4
4
4
Deleted.
Press any key to continue...

Somewhere, it should be printing "Out of scope."
The fact that the d'tor printed ANYTHING shows that the d'tor
destructed one SmrtPtr object. Which one, I don't know for
sure, but I'm guessing it was tuple in math(). If you're
expecting it to tell you when it gets through destructing
all the SmrtPtr objects, then OK, but you're going to have
to get rid of that "system("PAUSE");", because the final
d'tor calls will occur only AFTER you return from main.
So if you're launching this from an icon, you'll never see
that message, because it will only flash on your screen for
a nanosecond!

So get rid of the "system("PAUSE");", launch a DOS window,
and launch the program from within the DOS window by typing
it's name. THEN you will see the final d'tor calls.
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 15 '06 #4

Robbie Hatley wrote:
"Protoman" wrote:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "SmrtPtr.hpp"
using namespace std;

struct Data
{
double plus;
double mult;
double pow;
};

SmrtPtr<Datamath(const double& val,const double& val2);
int main()
{
for(;;)
{
cout << "Enter two values: " << endl;
double val, val2;
cin >val >val2;
SmrtPtr<Datatuple=math(val,val2);
cout << tuple->plus << endl << tuple->mult << endl << tuple->pow <<
endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

SmrtPtr<Datamath(const double& val,const double& val2)
{
SmrtPtr<Datatuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}
>
~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase; cout << "Deleted." << endl;}
else {delete DataBase; cout << "Out of scope. " << endl;}
}
}
>
What does it print when you run it?
OK, here's the output:

Enter two values:
2 2
4
4
4
Deleted.
Press any key to continue...

Somewhere, it should be printing "Out of scope."

The fact that the d'tor printed ANYTHING shows that the d'tor
destructed one SmrtPtr object. Which one, I don't know for
sure, but I'm guessing it was tuple in math(). If you're
expecting it to tell you when it gets through destructing
all the SmrtPtr objects, then OK, but you're going to have
to get rid of that "system("PAUSE");", because the final
d'tor calls will occur only AFTER you return from main.
So if you're launching this from an icon, you'll never see
that message, because it will only flash on your screen for
a nanosecond!

So get rid of the "system("PAUSE");", launch a DOS window,
and launch the program from within the DOS window by typing
it's name. THEN you will see the final d'tor calls.
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/'
I'll try that.

Jul 15 '06 #5

"Protoman" <Pr**********@gmail.comskrev i meddelandet
news:11*********************@i42g2000cwa.googlegro ups.com...

snip
SmrtPtr<Datatuple=math(val,val2);
snip
>
SmrtPtr<Datamath(const double& val,const double& val2)
{
SmrtPtr<Datatuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}
The short answer is: It might, but it doesn't have to.
This is an example of the Named Return Value Optimization (NRVO)
specifically allowed by the language standard. The math function
creates a local variable, that is only used to initialize an outer
variable. In that case, the compiler is allowed to skip the local
variable and construct the result directly into the outer variable.
Bo Persson

Jul 15 '06 #6

Bo Persson wrote:
"Protoman" <Pr**********@gmail.comskrev i meddelandet
news:11*********************@i42g2000cwa.googlegro ups.com...

snip
SmrtPtr<Datatuple=math(val,val2);

snip

SmrtPtr<Datamath(const double& val,const double& val2)
{
SmrtPtr<Datatuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}

The short answer is: It might, but it doesn't have to.
This is an example of the Named Return Value Optimization (NRVO)
specifically allowed by the language standard. The math function
creates a local variable, that is only used to initialize an outer
variable. In that case, the compiler is allowed to skip the local
variable and construct the result directly into the outer variable.
Bo Persson
I think Dev-Cpp is doing that, then. But wait, how can it skip the
construction of tuple; it's members are being assigned to. If it skips
th construction of tuple, what is being assigned to then?

Jul 15 '06 #7

"Protoman" <Pr**********@gmail.comskrev i meddelandet
news:11********************@35g2000cwc.googlegroup s.com...
>
Bo Persson wrote:
>"Protoman" <Pr**********@gmail.comskrev i meddelandet
news:11*********************@i42g2000cwa.googlegr oups.com...

snip
SmrtPtr<Datatuple=math(val,val2);

snip
>
SmrtPtr<Datamath(const double& val,const double& val2)
{
SmrtPtr<Datatuple(new Data);
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return tuple;
// should the d-tor get called here?!?
}

The short answer is: It might, but it doesn't have to.
This is an example of the Named Return Value Optimization (NRVO)
specifically allowed by the language standard. The math function
creates a local variable, that is only used to initialize an outer
variable. In that case, the compiler is allowed to skip the local
variable and construct the result directly into the outer variable.
Bo Persson

I think Dev-Cpp is doing that, then. But wait, how can it skip the
construction of tuple; it's members are being assigned to. If it
skips
th construction of tuple, what is being assigned to then?
It is being assigned to the outer tuple variable (in main), where the
result will end up anyway. You can think of it as-if it was written
(almost) like this:

void math(const double& val,const double& val2, SmrtPtr<Data>& tuple)
{
tuple = new Data;
tuple->plus=val+val2;
tuple->mult=val*val2;
tuple->pow=pow(val,val2);
return;
// no d-tor gets called here!
}
Bo Persson
Jul 15 '06 #8
Protoman wrote:

[snip]
Bo Persson wrote:
>>
The short answer is: It might, but it doesn't have to.

This is an example of the Named Return Value Optimization (NRVO)
specifically allowed by the language standard. The math function
creates a local variable, that is only used to initialize an outer
variable. In that case, the compiler is allowed to skip the local
variable and construct the result directly into the outer variable.

I think Dev-Cpp is doing that, then. But wait, how can it skip the
construction of tuple; it's members are being assigned to. If it skips
th construction of tuple, what is being assigned to then?
Of course it will still have to construct the object. What Bo Persson
meant was that, instead of creating a local object and returning a copy
of it to the caller, an implementation is allowed to provide an
optimization where a single copy of the object is created, where the
outgoing variable would be. This way, the object manipulated within the
function and that which will be returned to the calling code are the
same, not copies.

Regards,

--
Ney André de Mello Zunino
Jul 15 '06 #9

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

Similar topics

27
by: tuvok | last post by:
Is it correct that the virtual dtor of base gets called implicitly? Here's some code to demonstrate what I mean: Class B has a virtual destructor, so has class D which is derived from B. Deleting...
6
by: Martin | last post by:
Hi, Since I went ASP.NET with my global.asa (making it a global.asax) the application events are called just fine as well as the Session_OnStart event but the Session_OnEnd event is not. What is...
1
by: Sally | last post by:
Just confused about when OnInit and Constructor gets called in the parent/child case. Page.Constructor() Page.AddParsedSubObject() <- do all the sub's constructors get called here?...
0
by: Vishal | last post by:
Hello, I have overrided the LoadViewState function in my webform. However when the page postsback the function is never called. Here is how I overrided it. Protected Overloads Sub...
4
by: Niels Dekker (no reply address) | last post by:
When calling swap as follows (as recommanded in Effective C++, 3rd Edition, by Scott Meyers), what swap is chosen to be called? using std::swap; swap(a, b); Suppose there is a global ::swap...
1
by: sean | last post by:
I'm trying to create "rubber-band" rectangles by overriding the OnPaint method to place rectangles on top of all graphic controls, but when I call Me.Invalidate() (when the user moves the mouse),...
7
by: R.A.M. | last post by:
Hello, I am writing ASP.NET application. I would like to use forms authenication. I created Login.aspx page and modified Web.config. Start page is Default.aspx. The problem is that Login.aspx is...
11
by: notbob | last post by:
I'm trying to learn how to program. I'm using: How to Think Like a Computer Scientist Learning with Python 2nd Edition Am I likely to receive any help, here, or is there another irc,...
6
by: suresh | last post by:
Hi Could you please tell why copy constructor is not called in the first line in main(), as mentioned in the text books. I used g++ version 4.1.2 on debian etch thanks suresh # include...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
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,...
0
Oralloy
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,...

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.