Temporary objects as arguments to constructor calls

S

szaki

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
 
V

Victor Bazarov

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

Victor Bazarov

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 said:
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
 
S

szaki

Victor said:
(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".

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
 
V

Victor Bazarov

[...]
(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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top