Returning *this in member functions

后端 未结 9 1271
无人共我
无人共我 2020-12-18 22:35

I recently used a library that allows the following type of syntax:

MyClass myObject;
myObject
    .setMember1(\"string value\")
    .setMember2(4.0f)
    .s         


        
9条回答
  •  情歌与酒
    2020-12-18 23:37

    It's called a fluent api. It's not bad practice, just a different style of programming.

    The biggest drawbacks (IMO) would be that since you are returning a reference to yourself, you can't return anything else and it can be difficult to debug fluent statements since they are seen by the compiler as one giant "line" of code.

提交回复
热议问题