473,657 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

true or false question

This is a true or false question; if false, please explain why it is
so:

In main(...), the call to Handler with an argument will first call the
default ctor of the class which returns a temporary object and, then it
will call the copy ctor to copy the temp object to the object in the
Handler definition argument. So all in all, it calls two ctors, the
default and the copy ctor, respectively. Now, if this is true, can
someone explain why the default ctor only gets called in my compiler -
even though I've set optimizations to "Disabled (/Od)" in my VSC++ 2005
IDE? Could it be that this optimization is not an option to turn off or
on and that's why only the default ctor is called?

class Foo {
public:
Foo(const Foo& string) {
std::cout<<"cop y-ctor"<<std::end l;
}

Foo() {
std::cout<<"def-ctor"<<std::end l;
}
};

void Handler(Foo string) {

}

int main(int argc, char** argv)
{
Handler(Foo());

return 0;
}

[p.s, I know the standard allows copy-ctors to be bypassed on unnamed
temp objects, which would explain my result but I've set optimizations
under properties to Disabled (/Od) ]
I appreciate any replies in advance.

Jul 2 '06 #1
4 1534

Xllx Relinet posted:

Handler(Foo());

This line does the following:

(1) It creates a non-const, R-value, name-less object which will be
destroyed at the end of the statement.
(2) The name-less object is passed by value to "Handler", resulting in
a copy-construction. The object born by copy-construction is destroyed
before "Handler" returns control to "main".
(3) The name-less object is destroyed after the first semi-colon in the
body of "main".
The compiler is free to elide construction of temporary objects.
Look for a compiler flag that resembles something like:

--no-elide-constructor
--

Frederick Gotham
Jul 2 '06 #2
Frederick Gotham wrote:
(1) It creates a non-const, R-value, name-less object which will be
destroyed at the end of the statement.
My Book TICPP (Thinking In CPP) says that temporary objects are const
by default. What's the deal?
>
The compiler is free to elide construction of temporary objects.

Look for a compiler flag that resembles something like:

--no-elide-constructor
I'll try looking for that setting. Any Ideas where I could find it
exactly in VC++ 2005 S-Edition?

Jul 2 '06 #3
Xllx Relient posted:
Frederick Gotham wrote:
> (1) It creates a non-const, R-value, name-less object which will
be
>destroyed at the end of the statement.

My Book TICPP (Thinking In CPP) says that temporary objects are const
by default. What's the deal?

Your book is broken. Remedy: A canister of petrol and a box of matches.

A temporary is non-const by default. If you want a const temporary, you
must explicitly specify that you want it to be const:

#include<string >

int main()
{
typedef std::string const ConstString;

ConstString(); /* This a const temporary */
}
Here's something I posted recently which explains it in greater detail:

http://groups.google.ie/group/comp.l...323091f?hl=en&
>The compiler is free to elide construction of temporary objects.

Look for a compiler flag that resembles something like:

--no-elide-constructor

I'll try looking for that setting. Any Ideas where I could find it
exactly in VC++ 2005 S-Edition?

None sorry, but I bet they do over on:

microsoft.publi c.vstudio.devel opment

--

Frederick Gotham
Jul 2 '06 #4

Frederick Gotham wrote:
Xllx Relient posted:
Frederick Gotham wrote:
(1) It creates a non-const, R-value, name-less object which will
be
destroyed at the end of the statement.
My Book TICPP (Thinking In CPP) says that temporary objects are const
by default. What's the deal?


Your book is broken. Remedy: A canister of petrol and a box of matches.

A temporary is non-const by default. If you want a const temporary, you
must explicitly specify that you want it to be const:

#include<string >

int main()
{
typedef std::string const ConstString;

ConstString(); /* This a const temporary */
}
Here's something I posted recently which explains it in greater detail:

http://groups.google.ie/group/comp.l...323091f?hl=en&
The compiler is free to elide construction of temporary objects.

Look for a compiler flag that resembles something like:

--no-elide-constructor
I'll try looking for that setting. Any Ideas where I could find it
exactly in VC++ 2005 S-Edition?


None sorry, but I bet they do over on:

microsoft.publi c.vstudio.devel opment

--

Frederick Gotham
Thanks a lot, Frederick Gotham. You were very helpful.

Jul 2 '06 #5

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

Similar topics

9
4772
by: lawrence | last post by:
In the following loop, at the end, the method debugNotes() is printing out some notes for me to read, so I can figure out what is going on in my script. Where I try to print out the value of the variable $noFile, it is print "1" instead of "true". Why does it do this? The loop is failing to run a second time, for reasons I can not fathom. debugNotes tells me that the count of $dirArray is 10, and the file hasn't been found yet, so I...
18
2868
by: Daniel Klein | last post by:
In Python 2.2 I use to have true = (1 == 1) false = not true This was at the recommendation of someone on this list some time ago. The reason (if I remember correctly) was that setting true = 1 false = 0
18
1432
by: Stanley J Mroczek | last post by:
I Set the EditCommandColumn to Visible=False to stop people who are not allowed to make any changes to a record. How can set it to Visible=true for some users? Please answer in VB Thanks Stan
14
2465
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a "counted" string and data in a binary file. Given... #include <stdio.h> int main(int argc, char *argv) { char bstring;
48
30111
by: Skybuck Flying | last post by:
Hi, I came across this C code which I wanted to understand etc it looked like this: if (-1) etc It made me wonder what the result would be... true or false ? In C and Delphi
59
4576
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True So I changed my test for the obvious "if type(v) is bool", but I still find it confusing that "0 in " returns True
30
3128
by: Jason | last post by:
I am fairly new to ASP--I have been using it about 2 months. I did these tests (below), and it doesn't make sense to me. False is equal to 0, and that's fine. True should be equal to 1, but it's not. Actually, True should be equal to anything but False, null, and 0. Is there a workaround for this? Or do I need to change all my comparisons to = 1 instead of = true? response.write True = 1 'prints False response.write True = 0 ...
90
3409
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
2
2576
by: yawnmoth | last post by:
<?php $var = 'test'; echo isset($var) ? 'true' : 'false'; echo '<br>'; echo isset($var) ? 'true' : 'false'; ?> Why does that result in this output?:
40
2711
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) on win32 Type "help", "copyright", "credits" or "license" for more
0
8395
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, 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...
0
8826
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, 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...
0
8732
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 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...
0
8605
tracyyun
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...
1
6166
isladogs
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...
0
5632
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();...
1
2726
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
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
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...

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.