Answer the question
In order to leave comments, you need to log in
How to make chains of calls to methods of one class?
Good afternoon!
I want to make it so that the class methods can be called in a chain, to the perimeter:
MyClass * myClass = new MyClass();
myClass->summ(10).retract(4).div(2).cast(1);
......
......
delete myClass;
MyClass &summ(int) { return *this; }
// ИЛИ
MyClass *summ(int) { return this; }
Answer the question
In order to leave comments, you need to log in
you can return either a reference or a pointer.
What will be the most correct in this case and why?
myClass->summ(10).retract(4).div(2).cast(1);
- return the link, you want myClass->summ(10)->retract(4)->div(2)->cast(1);
- return the pointer. Otherwise, there is no difference.Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question