How to generate a compiler warning/error when object sliced

后端 未结 8 1593
执笔经年
执笔经年 2021-02-04 03:58

I want to know if it is possible to let compiler issue a warning/error for code as following:

Note:

1. Yea, it is bad programming style and

8条回答
  •  面向向阳花
    2021-02-04 04:02

    Not really a solution to your immediate problem, but....

    Most functions that take class/struct objects as parameters should declare the parameters to be of type "const X&" or "X&", unless they have a very good reason not to.

    If you always do this, object slicing will never be a problem (references don't get sliced!).

提交回复
热议问题