By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

true or false question

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.