Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to write assignment operator for a class with constant member variable?

How to write assignment operator for a class with constant member variable?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasetutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ComplexLifeForm
    wrote on last edited by
    #1

    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:

    C S 2 Replies Last reply
    0
    • C ComplexLifeForm

      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:

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      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. <

      1 Reply Last reply
      0
      • C ComplexLifeForm

        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:

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        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.

        C 1 Reply Last reply
        0
        • S Stuart Dootson

          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.

          C Offline
          C Offline
          ComplexLifeForm
          wrote on last edited by
          #4

          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 :) :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups