How to generate a compiler warning/error when object sliced

后端 未结 8 1697
执笔经年
执笔经年 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:03

    If you can modify the base class you could do something like:

    class Base
    {
    public:
    // not implemented will cause a link error
        Base(const Derived &d);
        const Base &operator=(const Derived &rhs);
    };
    

    Depending on your compiler that should get you the translation unit, and maybe the function where the slicing is happening.

提交回复
热议问题