Compiler Warning (level 4) C4512



 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 (or reference variable), the default assignment operator would not work.

#include
#include

class A {
public:
       A(int b) : a(b) , mb(b) {}

private:
      int &mb; // Constant reference
      const int a; // Constant variable

};

void main()
{
       A b(4512);
       std::cout<<b.a;
       _getch();
}



Solution :
a). Remove constness of data memebers if u can so that compiler is able to generate default assignment operator.
b). Give your own version of assignment operator (Override it in your class.).

Comments

Popular Posts