Method Chaining.

ob.fun1().fun2():

This way of calling functions is called Method Chaining. I have seen this using in big projects quite often. Here 1st ob.fun1() will be called which will return the some object. Then this object's fun2() will be called.

The below example is to illustrate the Method Chaining.

Eg:
=================================================================
class A
{
    int x;
    public:
        A() {x=10; }
        int getX() { return x;}
};

class B
{
    int y;
    public:
        B() { y=20; }
        A fun2();
};

A B::fun2()
{
    A o;
    return o;
}

int main()
{
    A ob;
    B ob2;
    cout<<  ob2.fun2().getX() ;
   return 0;
}

=================================================================

Output:
10

Comments

Vijay Koteswar said…
The below example is just to illustrate the Method Chaining.

Eg:
==============================
class A
{
int x;
public:
A() {x=10; }
int getX() { return x;}
};

class B
{
int y;
public:
B() { y=20; }
A fun2();
};

A B::fun2()
{
A o;
return o;
}

int main()
{
A ob;
B ob2;

cout<< ob2.fun2().getX();
return 0;
}
==============================
Vijay Koteswar said…
The output of the above eg is
10

Popular posts from this blog

GCC Extenstion

What is Code Bloat?