true or false question

  • Thread starter xllx.relient.xllx
  • Start date
X

xllx.relient.xllx

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.
 
F

Frederick Gotham

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
 
X

xllx.relient.xllx

Frederick said:
(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?
 
F

Frederick Gotham

Xllx Relient posted:
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.lang.c++/msg/574bfe911323091f?hl=en&


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
 
X

xllx.relient.xllx

Frederick said:
Xllx Relient posted:



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.lang.c++/msg/574bfe911323091f?hl=en&





None sorry, but I bet they do over on:

microsoft.public.vstudio.development

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top