473,503 Members | 1,638 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<<"copy-ctor"<<std::endl;
}

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

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 1527

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.public.vstudio.development

--

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.public.vstudio.development

--

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
4762
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...
18
2857
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 ...
18
1420
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
2441
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...
48
30028
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
4532
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 ...
30
3098
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...
90
3364
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
2563
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
2688
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...
0
7280
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,...
1
6991
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...
0
7462
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...
0
5578
agi2029
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,...
1
5014
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...
0
4673
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...
0
1512
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 ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
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...

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.