unfamilar C++ call syntax

M

Mark

I've seen this fragment of code used in the Boost libraries.
Can someone tell me what property of C++ this uses as I
have'nt seen it before.

I'm referring to the multiple () calls

is it similar to

desc.add_options().add_options("help","produce help
message").add_options("compression",po::value<int>(),"set compression
level");

??

// Declare the supported options.
po::eek:ptions_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("compression", po::value<int>(), "set compression level")
;
 
V

Victor Bazarov

Mark said:
I've seen this fragment of code used in the Boost libraries.
Can someone tell me what property of C++ this uses as I
have'nt seen it before.

I'm referring to the multiple () calls

is it similar to

desc.add_options().add_options("help","produce help
message").add_options("compression",po::value<int>(),"set compression
level");

??

// Declare the supported options.
po::eek:ptions_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("compression", po::value<int>(), "set compression level")
;

If a member function returns an object or a reference to an object, then
you can use the dot operator to access other members of that object.

struct A {
A& foo() { return *this; }
void bar() {}
};

int main() {
A a;

a.foo().bar(); // the same as ( a.foo() ).bar();
}

V
 
J

Jonathan Mcdougall

Mark said:
I've seen this fragment of code used in the Boost libraries.
Can someone tell me what property of C++ this uses as I
have'nt seen it before.

I'm referring to the multiple () calls

is it similar to

desc.add_options().add_options("help","produce help
message").add_options("compression",po::value<int>(),"set compression
level");

add_option() probably returns a reference to
'desc', making possible to chain the function calls.

class C
{
public:
C& f()
{
return *this;
}
};

int main()
{
C c;

c.f().f().f().f();
}
??

// Declare the supported options.
po::eek:ptions_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("compression", po::value<int>(), "set compression level")
;

Same thing here, excepts that the object returned
is a function object (either a real function or a
class with operator() ).

class FO
{
public:
FO& operator() ()
{
return *this;
}
};

int main()
{
FO o;

o()()()()()();
}


Jonathan
 

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
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top