Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved How to overload assignment operator inside a class?

    General and Desktop
    2
    3
    119
    Loading More Posts
    • 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.
    • CJha
      CJha last edited by

      Hi! I have a struct that has two members and I want to assign the current value of newState to oldState before changing the value of newState and then emit a signal with the new state as a parameter. My struct looks like this:

      /* myData.h */
      enum class myState_Enum{Uninitialized, OFF, ON, Processing};
      struct myStruct
      {
          myState_Enum oldState,
          myState_Enum newState,
      };
      
      

      The only way I am currently able to override the assignment operator is like this:

      /* myData.h */
      enum class myState_Enum{Uninitialized, OFF, ON, Processing};
      struct myStruct
      {
          myState_Enum oldState,
          myState_Enum newState,
          myStruct &operator=(myStruct val)
          {
              //perform the assignment here
              return *this;
          }
      };
      
      

      But, it defeats the purpose of overloading the assignment operator for me. I want to overload this in my class where this struct is a member so that I can emit a signal each time the newState is changed. I want to do something like this:

      /* mainData.h */
      class mainData
      {
      public:
          mainData();
          myStruct State;
          void operator=(myState_Enum val);
      
      signals:
          void stateChanged(myState_Enum val);
      }
      
      
      /*mainData.cpp */
      mainData::mainData()
      {
          State.oldState = myState_Enum::Uninitialized;
          State.newState = myState_Enum::Uninitialized;
      }
      
      void mainData::operator=(myState_Enum val)
      {
          State.oldState = State.newState;
          State.newState = val;
          emit stateChanged(val);
      }
      

      Is there any way I can achieve this? Thanks :)

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Not directly as you want. The usual way is to have a setter that does the value check and emit the signal if appropriate.

        Emitting a signal requires the class to derive from QObject which is not copyable.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        CJha 1 Reply Last reply Reply Quote 2
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Not directly as you want. The usual way is to have a setter that does the value check and emit the signal if appropriate.

          Emitting a signal requires the class to derive from QObject which is not copyable.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          CJha 1 Reply Last reply Reply Quote 2
          • CJha
            CJha @SGaist last edited by

            @SGaist Thank you :)

            1 Reply Last reply Reply Quote 0
            • First post
              Last post