2010年5月17日 星期一

Warning C4512 assignment operator could not be generated

The compiler will also generate an assignment operator function for a class that does not define one. This assignment operator is simply a member wise copy of the data members of an object. Because const data items cannot be modified after initialization, if the class contains a const item, the default assignment operator would not work.




You can resolve the C4512 warning for this code in one of three ways:



•Explicitly define an assignment operator for the class.



•Remove const from the data item in the class.



•Use the #pragma warning statement to suppress the warning.


這個Warning (Level 4)是因為編譯器會自動產生 default assignment operator
但是如果 class 裡面有 const data member,  因為 const data member 在 initialize 之後
就無法改變( assign )其值,所以沒有辦法產生default assignment operator。

沒有留言: