View Single Post
Go4Expert Member
28Jul2007,13:26  
oleber's Avatar
You need to return a reference , otherwise you are doing a copy of the object .
Try to add the method:

Code: CPP
Test::Test(Test& test) {
    cout<<"Test::Test(Test& test)\n";
}

Better shall be:

Code: CPP
#include <iostream>
using namespace std;Correct should be:
class Test
{
   
public:
    Test() {};
    Test(Test& test);
    Test& Display(); // Function 1
    Test& Show();    // Function 2
    Test& Result()// Function 3
};

Test::Test(Test& test) {
    cout<<"Test::Test(Test& test)\n";
}

Test& Test::Display()
{
    cout<<"Function 1"<<endl;
    return *this// Return this pointer
}

Test& Test::Show()
{
    cout<<"Function 2"<<endl;
    return *this;
}

Test& Test::Result()
{
    cout<<"Result 3"<<endl;
    return *this;
}

int main()
{
    Test Obj;
   
    //Here i have called all the three function in a sequence.   
    Obj.Display().Show().Result(); //This is called Function Chaining
}