Instantiation

N

Neelesh

Hi all,
Probably this question is a result of not understanding something very
basic -

class X{ };
int main()
{
X(f);
}

This code compiles without a problem and the line X(f) constructs a new
object of name 'f'.
I always believed that the expression X(f) creates a new temporary of
type X from a previously existing object f. Atleast that is what is
done when I say

X(f) + X(f) // assuming + is overloaded and some f exists and there is
a constructor inside X which can take an object of type f.

So does X(f) mean different things in different contexts? Can it create
a new object 'f' of type X as shown in above example?

Neelesh
 
V

Victor Bazarov

Neelesh said:
Probably this question is a result of not understanding something very
basic -

class X{ };
int main()
{
X(f);
}

This code compiles without a problem and the line X(f) constructs a new
object of name 'f'.
I always believed that the expression X(f) creates a new temporary of
type X from a previously existing object f. Atleast that is what is
done when I say

X(f) + X(f) // assuming + is overloaded and some f exists and there is
a constructor inside X which can take an object of type f.

So does X(f) mean different things in different contexts? Can it create
a new object 'f' of type X as shown in above example?

Yes. It does mean different things in different contexts. The main one you
should probably consider is "if it can be interpreted as a declaration, it
_will_ be". In a declaration, parentheses sometimes are superfluous. Can
we interpret

X(f);

as a declaration? Drop the parentheses, and you'll see.

In an expression, however, the existence of other things prohibit from
interpreting it as a declaration. In 'X(f) + X(f)', the plus basically
prevents the compiler from thinking it's a declaration.

HTH

V
 
M

Mike Wahler

Neelesh said:
Hi all,
Probably this question is a result of not understanding something very
basic -

class X{ };
int main()
{
X(f);
}

This code compiles without a problem and the line X(f) constructs a new
object of name 'f'.

Yes, although your extraneous parentheses needlessly
obscure the code. I'd have written it as:

X f;

I always believed that the expression X(f) creates a new temporary of
type X from a previously existing object f.

It would if such an object of type 'X' existed. However
your program has no such object in scope. Your code
creates a *new* object named 'f'. Its lifetime is from
its point of definition up to the end of 'main()'.
There's no temporary object involved.
Atleast that is what is
done when I say

X(f) + X(f) // assuming + is overloaded
and some f exists
:)


and there is
a constructor inside X which can take an object of type f.

There is an implicit copy ctor, since you didn't define one.
But that's not the issue here (see above)
So does X(f) mean different things in different contexts?

Yes. Many many things in C++ depend upon context.
As a matter of fact, I'm fond of telling folks:
"Without context, any 'knowledge' is meaningless." :)
Can it create
a new object 'f' of type X as shown in above example?

That's exactly what your code does. But lose the parentheses,
I think that's what caused your confusion in the first place.

Here's how to create a temporary 'X' object:

X();

-Mike
 

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,770
Messages
2,569,586
Members
45,083
Latest member
SylviaHarr

Latest Threads

Top