Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Temporary objects as arguments to constructor calls

Reply
Thread Tools

Temporary objects as arguments to constructor calls

 
 
szaki@colbud.hu
Guest
Posts: n/a
 
      06-21-2005
Hi,

I have tested the piece of code below on Intel ICC and GCC compilers,
both produce the same result.

--------------------
CODE:
--------------------

#include <iostream>
using namespace std;

class X
{
public:
X() {cout << "X constructor" << endl;}
};

class B
{
public:
B(const X &x) {cout << "B constructor" << endl;}
};

int main()
{
cout << "step 1" << endl;
B b1(X());
cout << "step 2" << endl;
B b2=B(X()); //works
return 0;
}

--------------
RESULT:
--------------

step 1
step 2
X constructor
B constructor

--------------

That is, in step 1 constructors are NOT CALLED! I cannot see why. Can
anybody explain it?

Thank you

Zoltan

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have tested the piece of code below on Intel ICC and GCC compilers,
> both produce the same result.
>
> --------------------
> CODE:
> --------------------
>
> #include <iostream>
> using namespace std;
>
> class X
> {
> public:
> X() {cout << "X constructor" << endl;}
> };
>
> class B
> {
> public:
> B(const X &x) {cout << "B constructor" << endl;}
> };
>
> int main()
> {
> cout << "step 1" << endl;
> B b1(X());


'b1' is a function. The statement above is a declaration of a function.
Read the FAQ.

> cout << "step 2" << endl;
> B b2=B(X()); //works
> return 0;
> }
>
> --------------
> RESULT:
> --------------
>
> step 1
> step 2
> X constructor
> B constructor
>
> --------------
>
> That is, in step 1 constructors are NOT CALLED! I cannot see why. Can
> anybody explain it?

 
Reply With Quote
 
 
 
 
szaki@colbud.hu
Guest
Posts: n/a
 
      06-22-2005
Hm, I see, thanks. I still think it is weird, but probably it has its
rationale.

Victor Bazarov írta: > (E-Mail Removed) wrote:
> > I have tested the piece of code below on Intel ICC and GCC compilers,
> > both produce the same result.
> >
> > --------------------
> > CODE:
> > --------------------
> >
> > #include <iostream>
> > using namespace std;
> >
> > class X
> > {
> > public:
> > X() {cout << "X constructor" << endl;}
> > };
> >
> > class B
> > {
> > public:
> > B(const X &x) {cout << "B constructor" << endl;}
> > };
> >
> > int main()
> > {
> > cout << "step 1" << endl;
> > B b1(X());

>
> 'b1' is a function. The statement above is a declaration of a function.
> Read the FAQ.
>
> > cout << "step 2" << endl;
> > B b2=B(X()); //works
> > return 0;
> > }
> >
> > --------------
> > RESULT:
> > --------------
> >
> > step 1
> > step 2
> > X constructor
> > B constructor
> >
> > --------------
> >
> > That is, in step 1 constructors are NOT CALLED! I cannot see why. Can
> > anybody explain it?


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-22-2005
(E-Mail Removed) wrote:
> Hm, I see, thanks. I still think it is weird, but probably it has its
> rationale.


(a) Please don't top-post

(b) If you _are_ interested in the rationale, it's been discussed
quite a few times, and I am fairly certain you can find it either
on the web or in the news archives. Look for "what looks like
a declaration is a declaration".

> Victor Bazarov írta: > (E-Mail Removed) wrote:
>
>>>I have tested the piece of code below on Intel ICC and GCC compilers,
>>>both produce the same result.
>>> [...]
>>>int main()
>>>{
>>> cout << "step 1" << endl;
>>> B b1(X());

>>
>>'b1' is a function. The statement above is a declaration of a function.
>>Read the FAQ.
>>
>>
>>> cout << "step 2" << endl;
>>> B b2=B(X()); //works
>>> return 0;
>>>}
>>>


V
 
Reply With Quote
 
szaki@colbud.hu
Guest
Posts: n/a
 
      06-24-2005
Victor Bazarov írta: > (E-Mail Removed) wrote:
> > Hm, I see, thanks. I still think it is weird, but probably it has its
> > rationale.

>
> (a) Please don't top-post
>
> (b) If you _are_ interested in the rationale, it's been discussed
> quite a few times, and I am fairly certain you can find it either
> on the web or in the news archives. Look for "what looks like
> a declaration is a declaration".
>
> > Victor Bazarov írta: > (E-Mail Removed) wrote:
> >


Dear Victor

(a) sorry about that, I haven't used much newsgroups, so I don't know
the ethics. But as you can see, I looked up what top-posting means.

(b) Yes, I am interested, unless it requires thorough knowledge of the
theory of C++ compilers. I'll look it up on the web. In fact, before
posting I had searched the web, the only thing I found mentioned this
'function declaration vs. variable initialization' ambiguity but only
in the context of empty constructors (e.g why string s(); does not
work).

(c) Since my last post I've found out that although B b1(X()); does not
work, B b1((X())); does, which makes the story even more weird.

Regards

Zoltan

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-24-2005
(E-Mail Removed) wrote:
> [...]
> (c) Since my last post I've found out that although B b1(X()); does not
> work, B b1((X())); does, which makes the story even more weird.


Since you've found the "ambiguity" thing, you must have seen how C++
resolves it. I actually mentioned it. What looks like a declaration *is*
a declaration. Now, according to C++ grammar rules, if the identifier is
followed by a parentheses after a type, it could be either a declaration
or an initialiser. A declaration would be of a function, and the format
of the formal argument list is quite strict: either empty or a list of
declarations. As soon as the compiler encounters the second opening
parenthesis ("B b1 ( _(_ ") then it sees that what's inside the first,
the outermost set of parentheses is not a list of formal arguments but
instead an expression. It makes a conclusion that what it has here is
an object definition/initialisation.

V
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Constructor And Temporary Objects Marcelo De Brito C++ 2 03-01-2009 06:21 AM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Declare array of objects with constructor having arguments b83503104@yahoo.com C++ 7 07-21-2005 06:41 AM
Temporary objects as constructor arguments? Andy Buckley C++ 5 08-26-2004 01:32 AM
Sequence of constructor calls and destructor calls. Honne Gowda A C++ 2 10-31-2003 09:31 AM



Advertisments