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

Instantiation

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

Oct 28 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Neelesh" <ne***********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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
Oct 28 '05 #2

P: n/a
Neelesh wrote:
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
Oct 28 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.