I have a class with a member which is not changed by the methods of the class, so I marked it as const
. My problem is that I was using the default assignment operator just like a copy constructor in order to avoid multiple declarations. But in this case the assignment operator is not automatically generated, so I get some compiler errors:
'operator =' function is unavailable
. This seems like that there is no real life scenario where const class members can be actually used (e.g. have you seen any const member in the STL code?).
Is there any way to fix this, beside removing the const
?
EDIT: some code
class A
{
public :
const int size;
A(const char* str) : size(strlen(str)) {}
A() : size(0) {}
};
A create(const char* param)
{
return A(param);
}
void myMethod()
{
A a;
a = create("abcdef");
// do something
a = create("xyz");
// do something
}