answersLogoWhite

0


Best Answer

Cascading refers to the following style of calling multiple methods in a sequence:

someObject.firstMethod().secondMethod().thirdMethod();

For this to work, firstMethod() returns an object on which secondMethod() is called, and similarly this returns an object on which thirdMethod() is called.

A commonly seen instance of this is the overloaded stream insertion operator - i.e. operator<<() - in the standard library. For example, you can write:

std::cout << "The meaning of life is " << 42 << std::endl;

Each invocation of operator<<() returns the std::cout object, allowing an arbitrarily long sequence of calls to be chained together.

Here's an example of a class which supports such cascading:

class MyClass {private:int data;

public:MyClass(int initData) : data(initData) {}

MyClass goUp() {data++;return *this;}

MyClass goDown() {data--;return *this;} };

With the above definition, we can write

MyClass obj;

obj.goUp().goUp().goDown();

and so on.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is meant by cascading in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp