c++ - Return void or reference to self? -


given following class:

struct object {     int x, y;     void addtoall( int value ){ x += value; y += value; };     object& addtoall( int value ){ x += value; y += value; return *this; }; }; 

what difference between 2 member functions?

i understand returning reference self required operator overloads (e.g: operator+= ), excluding operator overloading, necessary? if not, when want or need return reference self opposed returning void?

i apologize if found via google-fu, or basic question, wasn't sure search (and not lack of trying).

what difference between 2 member functions?

the function returning reference instance can chained when called like

object o; o.addtoall(5).addtoall(6).addtoall(7); 

if useful depends on actual use case, it's used develop called domain specific language syntax.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -