How to write assignment operator for a class with constant member variable?
-
Hi, I have a class with constant member variable. As constant member variables MUST be initialized in the constructor initializer list, how do I write an assignment operator for such a class. E.g. class MyClass { public: MyClass() : m_ConstantMember(0) - (1) {} MyClass(int intvar) : m_ConstantMember(intvar) - (2) {} MyClass(MyClass const& rhs) - (3) : m_ConstantMember(rhs.m_ConstantMember) {} MyClass& operator = (MyClass const& rhs) -- (4) { if(this != &rhs) { m_ConstantMember = rhs.m_ConstantMember; -------- (4a) //memcpy((void*)&m_ConstantMember, (void*)(int*)&rhs.m_ConstantMember, sizeof(m_ConstantMember)) ------------ (4b) } return *this; } private: int const m_ConstantMember; }; The above class has one constant member variable of type int. This variable MUST be initialized in the constructor initializer list. This is achieved in the above class by 1. No argument constructor 2. One argument constructor 3. Copy Constructor For the assignment operator, if I write a statment as in 4a, the compiler complains. However if I write the statement as in 4b, the compiler is happy and I get the desired result. So my query is whether the statement 4b is the correct way of writing an assignment operator for a class with constant member variable? If not can someone please tell me the correct way of writing the assignment operator. Thanks and Regards. :confused::confused:
-
Hi, I have a class with constant member variable. As constant member variables MUST be initialized in the constructor initializer list, how do I write an assignment operator for such a class. E.g. class MyClass { public: MyClass() : m_ConstantMember(0) - (1) {} MyClass(int intvar) : m_ConstantMember(intvar) - (2) {} MyClass(MyClass const& rhs) - (3) : m_ConstantMember(rhs.m_ConstantMember) {} MyClass& operator = (MyClass const& rhs) -- (4) { if(this != &rhs) { m_ConstantMember = rhs.m_ConstantMember; -------- (4a) //memcpy((void*)&m_ConstantMember, (void*)(int*)&rhs.m_ConstantMember, sizeof(m_ConstantMember)) ------------ (4b) } return *this; } private: int const m_ConstantMember; }; The above class has one constant member variable of type int. This variable MUST be initialized in the constructor initializer list. This is achieved in the above class by 1. No argument constructor 2. One argument constructor 3. Copy Constructor For the assignment operator, if I write a statment as in 4a, the compiler complains. However if I write the statement as in 4b, the compiler is happy and I get the desired result. So my query is whether the statement 4b is the correct way of writing an assignment operator for a class with constant member variable? If not can someone please tell me the correct way of writing the assignment operator. Thanks and Regards. :confused::confused:
Why are you trying to chqange the value of a const? Constants are constants because their value is constant, it does not change during their lifetime. The compiler complains about it because basicly you are telling it to: "make a constant whose value never changes once it is set, and now chane it.". if you want to change the value then don't make it const.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
-
Hi, I have a class with constant member variable. As constant member variables MUST be initialized in the constructor initializer list, how do I write an assignment operator for such a class. E.g. class MyClass { public: MyClass() : m_ConstantMember(0) - (1) {} MyClass(int intvar) : m_ConstantMember(intvar) - (2) {} MyClass(MyClass const& rhs) - (3) : m_ConstantMember(rhs.m_ConstantMember) {} MyClass& operator = (MyClass const& rhs) -- (4) { if(this != &rhs) { m_ConstantMember = rhs.m_ConstantMember; -------- (4a) //memcpy((void*)&m_ConstantMember, (void*)(int*)&rhs.m_ConstantMember, sizeof(m_ConstantMember)) ------------ (4b) } return *this; } private: int const m_ConstantMember; }; The above class has one constant member variable of type int. This variable MUST be initialized in the constructor initializer list. This is achieved in the above class by 1. No argument constructor 2. One argument constructor 3. Copy Constructor For the assignment operator, if I write a statment as in 4a, the compiler complains. However if I write the statement as in 4b, the compiler is happy and I get the desired result. So my query is whether the statement 4b is the correct way of writing an assignment operator for a class with constant member variable? If not can someone please tell me the correct way of writing the assignment operator. Thanks and Regards. :confused::confused:
Ask yourself these questions: 1) Why does the const member *have* to be const? If you made it non-const, life would be a lot simpler for you. What's the rationale for const? 2) Why do you need an assignment operator? An assignment operator makes little or no sense when part of the class is const. My gut feel is that when you're trying to work around things like this, something in the design is out of kilter and needs fixing.
-
Ask yourself these questions: 1) Why does the const member *have* to be const? If you made it non-const, life would be a lot simpler for you. What's the rationale for const? 2) Why do you need an assignment operator? An assignment operator makes little or no sense when part of the class is const. My gut feel is that when you're trying to work around things like this, something in the design is out of kilter and needs fixing.
Hello Guys, Thanks a lot for your response. As far as the class goes it's not a design issue. I just wanted to find out how to write a proper an assignment operator of for a class with contant member. So it was just out of curosity that I wrote a sample class. Regards :) :)