Hey,
In the book <Effective C++>, the author provides an example to prove
why we need "pass by reference". I redoed the example, and found
something interesting. The codes are:
############### ###############
#include <iostream>
class Student{
public:
Student(){
std::cout << "inside CTOR. this = " << this << std::endl <<
std::endl;
}
Student(const Student& rhs){
std::cout << "inside COPY CTOR. this = " << this << ", rhs =
"<< &rhs << std::endl << std::endl;
}
~Student(){
std::cout << "inside DTOR. this = " << this << std::endl <<
std::endl;
}
};
Student ReturnStudent(S tudent s){
std::cout << "inside 'ReturnStudent' function" << std::endl <<
std::endl;
return s;
}
int main(){
Student Plato;
ReturnStudent(P lato);
std::cout << "outside 'ReturnStudent' function" << std::endl <<
std::endl;
}
############### ############
The thing is when you run it, I got the following output:
inside CTOR. this = 0xbfff6e30
inside COPY CTOR. this = 0xbfff6e10, rhs = 0xbfff6e30
inside 'ReturnStudent' function
inside COPY CTOR. this = 0xbfff6e20, rhs = 0xbfff6e10
inside DTOR. this = 0xbfff6e20
inside DTOR. this = 0xbfff6e10
outside 'ReturnStudent' function
inside DTOR. this = 0xbfff6e30
############### ############### ##
Which means DTOR of the returned value called actually 'BEFORE' DTOR of
's' called inside the ReturnStudent function, and this is differenct to
the author said, and which I originally believed.
I am using G++ 3.2.3, Linux 2.4.21-32.EL. Compile the program using
"g++ -g main.cpp", not without any optimization. 3 1568
* Zongjun Qi:
Hey,
In the book <Effective C++>, the author provides an example to prove
why we need "pass by reference". I redoed the example, and found
something interesting. The codes are:
############### ###############
#include <iostream>
class Student{
public:
Student(){
std::cout << "inside CTOR. this = " << this << std::endl <<
std::endl;
}
Student(const Student& rhs){
std::cout << "inside COPY CTOR. this = " << this << ", rhs =
"<< &rhs << std::endl << std::endl;
}
~Student(){
std::cout << "inside DTOR. this = " << this << std::endl <<
std::endl;
}
};
Student ReturnStudent(S tudent s){
std::cout << "inside 'ReturnStudent' function" << std::endl <<
std::endl;
return s;
}
int main(){
Student Plato;
ReturnStudent(P lato);
std::cout << "outside 'ReturnStudent' function" << std::endl <<
std::endl;
}
############### ############
The thing is when you run it, I got the following output:
inside CTOR. this = 0xbfff6e30
inside COPY CTOR. this = 0xbfff6e10, rhs = 0xbfff6e30
inside 'ReturnStudent' function
inside COPY CTOR. this = 0xbfff6e20, rhs = 0xbfff6e10
inside DTOR. this = 0xbfff6e20
inside DTOR. this = 0xbfff6e10
outside 'ReturnStudent' function
inside DTOR. this = 0xbfff6e30
############### ############### ##
Which means DTOR of the returned value called actually 'BEFORE' DTOR of
's' called inside the ReturnStudent function, and this is differenct to
the author said, and which I originally believed.
I am using G++ 3.2.3, Linux 2.4.21-32.EL. Compile the program using
"g++ -g main.cpp", not without any optimization.
I don't believe Scott Meyers have written that the output must be
different from you got above. It's much more likely that you have
misinterpreted what he wrote. Here are the rules:
* Destructors of automatic objects (all objects in your program are
automatic) are called in opposite order of construction.
* Destructors of static objects are also called in opposite order of
construction.
* If you have both automatic and static objects, then the two rules
above allow, as a logical consequence of the possibility of local
static objects, that constructions and destructions sequences
such as A(), B(), ~A(), ~B() can occur, i.e. not strictly nested.
In your program destructors are called in opposite order of construction.
That's correct behavior.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf,
Thanks for your explanation. That explains well the output from G++.
However, what Meyer says in the book is a little differenct as I stated
above. But THAT' S OKAY. I guess that's a minor mistake. I think you
are right in listing the right order.
Thanks,
Zongjun
On Nov 14, 3:43 pm, "Alf P. Steinbach" <a...@start.now rote:
* Zongjun Qi:
Hey,
In the book <Effective C++>, the author provides an example to prove
why we need "pass by reference". I redoed the example, and found
something interesting. The codes are:
############### ###############
#include <iostream>
class Student{
public:
Student(){
std::cout << "inside CTOR. this = " << this << std::endl <<
std::endl;
}
Student(const Student& rhs){
std::cout << "inside COPY CTOR. this = " << this << ", rhs =
"<< &rhs << std::endl << std::endl;
}
~Student(){
std::cout << "inside DTOR. this = " << this << std::endl <<
std::endl;
}
};
Student ReturnStudent(S tudent s){
std::cout << "inside 'ReturnStudent' function" << std::endl <<
std::endl;
return s;
}
int main(){
Student Plato;
ReturnStudent(P lato);
std::cout << "outside 'ReturnStudent' function" << std::endl <<
std::endl;
}
############### ############
The thing is when you run it, I got the following output:
inside CTOR. this = 0xbfff6e30
inside COPY CTOR. this = 0xbfff6e10, rhs = 0xbfff6e30
inside 'ReturnStudent' function
inside COPY CTOR. this = 0xbfff6e20, rhs = 0xbfff6e10
inside DTOR. this = 0xbfff6e20
inside DTOR. this = 0xbfff6e10
outside 'ReturnStudent' function
inside DTOR. this = 0xbfff6e30
############### ############### ##
Which means DTOR of the returned value called actually 'BEFORE' DTOR of
's' called inside the ReturnStudent function, and this is differenct to
the author said, and which I originally believed.
I am using G++ 3.2.3, Linux 2.4.21-32.EL. Compile the program using
"g++ -g main.cpp", not without any optimization.I don't believe Scott Meyers have written that the output must be
different from you got above. It's much more likely that you have
misinterpreted what he wrote. Here are the rules:
* Destructors of automatic objects (all objects in your program are
automatic) are called in opposite order of construction.
* Destructors of static objects are also called in opposite order of
construction.
* If you have both automatic and static objects, then the two rules
above allow, as a logical consequence of the possibility of local
static objects, that constructions and destructions sequences
such as A(), B(), ~A(), ~B() can occur, i.e. not strictly nested.
In your program destructors are called in opposite order of construction.
That's correct behavior.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Zongjun Qi wrote:
Hey,
In the book <Effective C++>, the author provides an example to prove
why we need "pass by reference". I redoed the example, and found
something interesting. The codes are:
############### ###############
#include <iostream>
class Student{
public:
Student(){
std::cout << "inside CTOR. this = " << this << std::endl <<
std::endl;
}
Student(const Student& rhs){
std::cout << "inside COPY CTOR. this = " << this << ", rhs =
"<< &rhs << std::endl << std::endl;
}
~Student(){
std::cout << "inside DTOR. this = " << this << std::endl <<
std::endl;
}
};
Student ReturnStudent(S tudent s){
std::cout << "inside 'ReturnStudent' function" << std::endl <<
std::endl;
return s;
}
int main(){
Student Plato;
ReturnStudent(P lato);
std::cout << "outside 'ReturnStudent' function" << std::endl <<
std::endl;
}
############### ############
The thing is when you run it, I got the following output:
inside CTOR. this = 0xbfff6e30
inside COPY CTOR. this = 0xbfff6e10, rhs = 0xbfff6e30
inside 'ReturnStudent' function
inside COPY CTOR. this = 0xbfff6e20, rhs = 0xbfff6e10
inside DTOR. this = 0xbfff6e20
inside DTOR. this = 0xbfff6e10
outside 'ReturnStudent' function
inside DTOR. this = 0xbfff6e30
############### ############### ##
Which means DTOR of the returned value called actually 'BEFORE' DTOR of
's' called inside the ReturnStudent function, and this is differenct to
the author said, and which I originally believed.
I am using G++ 3.2.3, Linux 2.4.21-32.EL. Compile the program using
"g++ -g main.cpp", not without any optimization.
The above is better explained by:
int main(){
Student Plato;
Student me = ReturnStudent(P lato);
std::cout << "outside 'ReturnStudent' function" << std::endl <<
std::endl;
}
The intended sequence of destruction now occurs correctly. Without the
Student me, the compiler just zaps both the dropped returned student
and the original parameter as fast as it can.
By the way, the author is not focusing on the sequence, its the fact
that Student, when copied, invokes the ctors for each one of its
members as well.
He's refering to the "cost" of passing by value. Members need to be
destroyed too. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Eshrath |
last post by:
Hi,
What I am trying to do:
=======================
I need to form a table in html using the xsl but the table that is
formed is quite long and cannot be viewed in our application. So we
are writing one object in C# which will take the entire table tag
contents and renders. Ie., we need to pass "<table>…………
<thead>……</thead>. <tr>.<td> <td>..<tr>.<td> <td> </table>" content to
|
by: Donald Firesmith |
last post by:
I am having trouble having Google Adsense code stored in XSL converted
properly into HTML. The <> unfortunately become < and > and then
no longer work.
XSL code is:
<script type="text/javascript">
<!]>
</script>
<script type="text/javascript"
|
by: red floyd |
last post by:
I have a suggestion for the standard library....
This is sort of a combination of std::transform() and std::for_each().
It applies the binary function to each iterator in
http://www.jamesd.demon.co.uk/csc/faq.html ]
|
by: Luke |
last post by:
Built-in functions don't bind to classes like regular functions. Is
this intended? (I do notice that the Python Reference Manual sec 3.2
under "Class Instance" refers to a "user-defined function"). Any ideas
what the reason is for this distinction between build-in functions and
normal functions?
It's rather inconvenient when implementing some methods (not the whole
class) in a C extension :-(
$ python
|
by: Robert Dodier |
last post by:
Hello,
Sorry for asking what must be a FAQ, but I wasn't able to find the
answer.
I have an XML document fragment which I want to store as a text string.
I want a function to convert any XML special characters such as < >
into
the corresponding character entities. I'm working with Java.
| |
by: Szabolcs Horvát |
last post by:
Consider the attached example program: an object of type 'A' is inserted
into a 'map<int, Am;'. Why does 'm;' call the copy constructor of
'A' twice in addition to a constructor call?
The constructors and copy constructors in 'A' report when they are
called. 'whoami' is just a unique identifier assigned to every object of
type 'A'. The output of the program is:
constructor 0
constructor 1
|
by: elderic |
last post by:
Hi there,
are there other ways than the ones below to check for <type 'function'>
in a python script?
(partly inspired by wrapping Tkinter :P)
def f():
print "This is f(). Godspeed!"
1.: --sort of clumsy and discouraged by the docs as far as I read
|
by: yancheng.cheok |
last post by:
Hello all, I try to figure out what is the sequence when we create and
delete an object. After experiment on my VC++ 2003, I found that here
is the sequence
new-constructor-destructor-delete
What I am concern is, is this sequence a C++ standard specification? Or
it is compiler dependent?
Thank you. Here is the code I use to perform experiment:
|
by: ajay2552 |
last post by:
Hi,
I have a query.
All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it?
One method is to use <, > ,<Company> to display '<', '>' and '<Company>' respectively.
But is there any freeware code available which could implement the above functionality without having to use >,< and such stuff???
|
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: 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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |