Syntax question

D

default user

Hi,


I am having trouble grasping the following syntax:

foo().bar()


Specifically, what are the parenthesis after foo doing?

Only thing I can think of is that they mean to construct an object of type foo.

Thanks for any light you can shed. I have been trying to figure this out.

Johann
 
D

Dietmar Kuehl

default said:
I am having trouble grasping the following syntax:

foo().bar()


Specifically, what are the parenthesis after foo doing?

This can mean different things, depending on the context of
the given expression:

- If 'foo' is a class name, 'foo()' creates a default constructed
temporary object.
- If 'foo' is a function name, 'foo()' calls the function.
- If 'foo' is an object name, 'foo()' calls the function call
operator.
 
J

Jerry Coffin

default said:
Hi,


I am having trouble grasping the following syntax:

foo().bar()


Specifically, what are the parenthesis after foo doing?

If foo is a class, they default-construct object of that class.

If foo isn't a class, it could be a function taking no parameters that
returns an object or reference to an object. 'foo()' calls that
function.

Either way, the '.bar()' then calls the bar member function of the
[referenced] object.
 
K

Karl Heinz Buchegger

default said:
Hi,

I am having trouble grasping the following syntax:

foo().bar()

Specifically, what are the parenthesis after foo doing?

Only thing I can think of is that they mean to construct an object of type foo.

Depends on context. Show a little bit more code.

Some quick shots:

* foo is a function, returning some object
then foo().bar()
calls the function foo, afterwards bar() is called for the object
returned by foo

* foo is a data type
then foo().bar()
creates a temporary foo object and calls member function bar() of it.

* foo is an object, which has operator() defined
then foo().bar()
calls operator() of that object and on the returned object the member function
bar() is called

I am quite sure that there are more possible interpretations depending on
what foo actually is.
 
M

Matthias Kaeppler

default said:
Hi,


I am having trouble grasping the following syntax:

foo().bar()


Specifically, what are the parenthesis after foo doing?

Only thing I can think of is that they mean to construct an object of type foo.

Thanks for any light you can shed. I have been trying to figure this out.

Johann

Not necessarily. Usually you see this kind of syntax if both foo and bar
are functions, whereas foo returns an object on which bar is invoked
(that means, bar operates on the return value of foo).
This is called method chaining.
 
C

Calum Grant

default said:
Hi,


I am having trouble grasping the following syntax:

foo().bar()

e.g.

struct A
{
void bar() { }
};

A foo() { return A(); }

foo().bar();

Specifically, what are the parenthesis after foo doing?

Depends on what foo is.
- Calling a function foo with no arguments
- Calling operator () on an object foo
- Creating a value of type foo.
 
D

default user

Hi,


I am having trouble grasping the following syntax:

foo().bar()


Specifically, what are the parenthesis after foo doing?

Thank you for the help. Matthias hit it on the
head it is being used in method chaining. I checked through the
FAQ yesterday but couldn't find that syntax. After reading Matthias's
answer, I googled for method chaining c++ and it pops up as
FAQ 8.4. Thanks again, have a good weekend.

Johann
 

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
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top