Concept behind Accessing Function via a Function: Object.function1().function2();

Generally we access a member function (say fun1()) of a class(say ClassA) as-

1. ClassA a; //Declare object of ClassA
2. a.fun1(); //It means fun1() is defined inside ClassA

But somewhere I have seen codes like -
3.a.fun1().fun2(); //But a function can never be defined inside a function.

For example in C# I can write-

4. string query = "select * from Hindi";
5. query.Trim().ToString();
So what is the concept behind code of line 3 or 5(callinga function by a function). Please provide any sample examples in c++.
If fun1() returns an object that has a method fun2(), you can use line3 to call fun2() directly from fun1() result
Thanx buddy to bring this idea in my mind!
This should be like-

#include<iostream.h>
#include<conio.h>

class B;
class A
{
int x;
public:
B fun1(B b)
{

return b;
}

};

class B
{

public:

int y;
void sety()
{
y=10;
cout<<y;
}

};

void main()
{
A a;
B b;

a.fun1(b).sety();

}

Typically you would return a reference, not a copy.

Here's a more typical example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class B
{
public:
  B& CallMe(int foo)
  {
    // do something
    return *this;
  }
};

int main()
{
  B b;

  // now this...
  b.CallMe(1).CallMe(2);

  // is the same as this:
  b.CallMe(1);
  b.CallMe(2);
}


Note that this is exactly how the << operator works with cout/iostream.
Topic archived. No new replies allowed.