are these statements declarations or definitions?

J

Jess

Hello,

If I have a statement

A a(B());

where A and B are both types, then it seems I can have two
interpretations:

1. B() is a casting operation and returns an object of type B, which
is then passed to A's constructor (taking a B object as argument) to
construct an A object. Hence the statement is a definition, defining
an object "a" of type A.

2. B() is the same as B(*)(), hence the statement is a function
declaration: "a" is a function, which takes a pointer to a function
(the function takes no arg and returns a result of type B) and returns
an object of type A.

Both seem reasonable, but which one of them is correct?

Similarly, a statement

A a(B (c));

also seems to have two interpretations, since I can either interpret
"B (c)" as a casting operation, or a pointer to a function.

I vaguely remember that the Standard says if a statement can be
interpreted as a declaration, then it is a declaration. If so, I
guess the statements above are declarations.

Thanks a lot,
Jess
 
V

Victor Bazarov

Jess said:
If I have a statement

[...]

I vaguely remember that the Standard says if a statement can be
interpreted as a declaration, then it is a declaration. If so, I
guess the statements above are declarations.

Yes, they are. You remember just the right thing.

V
 
J

Jess

Jess said:
If I have a statement

I vaguely remember that the Standard says if a statement can be
interpreted as a declaration, then it is a declaration. If so, I
guess the statements above are declarations.

Yes, they are. You remember just the right thing.

V


Thanks. If I'd like to define a shared_ptr object using

std::tr1::shared_ptr<A> pA(createA());

where createA() returns a A*, then is this still a declaration?
Again, it can be interpreted as a declaration and a definition, but
clearly, I'd like to define pA as a shared_ptr<A> object.

Thanks,
Jess
 
V

Victor Bazarov

Jess said:
[..] If I'd like to define a shared_ptr object using

std::tr1::shared_ptr<A> pA(createA());

where createA() returns a A*, then is this still a declaration?

If 'createA' is a function (and not a type), then it cannot be
interpreted as a declaration.
Again, it can be interpreted as a declaration and a definition, but
clearly, I'd like to define pA as a shared_ptr<A> object.

If 'createA' is a type, and you intend to use a temporary of that
type to do what you need, then you shouldn't use the parenthesised
form, but instead use the assignment form:

... pA = createA();

V
 
J

Jess

Jess said:
[..] If I'd like to define a shared_ptr object using
std::tr1::shared_ptr<A> pA(createA());
where createA() returns a A*, then is this still a declaration?

If 'createA' is a function (and not a type), then it cannot be
interpreted as a declaration.
Again, it can be interpreted as a declaration and a definition, but
clearly, I'd like to define pA as a shared_ptr<A> object.

If 'createA' is a type, and you intend to use a temporary of that
type to do what you need, then you shouldn't use the parenthesised
form, but instead use the assignment form:

... pA = createA();

V


Thanks for the tips. :)
Jess
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top