Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to generate a compiler warning/error when object sliced
    text
    copied!<p>I want to know if it is possible to let compiler issue a warning/error for code as following:</p> <p><strong>Note:</strong></p> <p><em>1. Yea, it is bad programming style and we should avoid such cases - but we are dealing with legacy code and hope compiler can help identify such cases for us.)</em></p> <p><em>2. I prefer a compiler option (VC++) to disable or enable object slicing, if there is any.</em></p> <pre><code>class Base{}; class Derived: public Base{}; void Func(Base) { } //void Func(Derived) //{ // //} //main Func(Derived()); </code></pre> <p>Here if I comment out the second function, the first function would be called - and the compiler (both VC++ and Gcc) feels comfortable with that.</p> <p>Is it C++ standard? and can I ask compiler (VC++) to give me a warning when met such code?</p> <p>Thanks so much!!!</p> <p><strong>Edit:</strong></p> <p>Thanks all so much for your help! </p> <p>I can't find a compiler option to give a error/warning - I even posted this in MSDN forum for VC++ compiler consultant with no answer. So I am afraid neither gcc nor vc++ implemented this feature.</p> <p>So add constructor which take derived classes as paramter would be the best solution for now.</p> <p><strong>Edit</strong></p> <p>I have submit a feedbak to MS and hope they will fix it soon:</p> <p><a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=421579" rel="noreferrer">https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=421579</a></p> <p>-Baiyan</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload