Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to overload assignment operator inside a class?
Forum Updated to NodeBB v4.3 + New Features

How to overload assignment operator inside a class?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 354 Views 2 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
    CJha
    wrote on 27 Jul 2020, 17:17 last edited by
    #1

    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
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 27 Jul 2020, 17:58 last edited by
      #2

      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

      C 1 Reply Last reply 27 Jul 2020, 18:05
      2
      • S SGaist
        27 Jul 2020, 17:58

        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.

        C Offline
        C Offline
        CJha
        wrote on 27 Jul 2020, 18:05 last edited by
        #3

        @SGaist Thank you :)

        1 Reply Last reply
        0

        1/3

        27 Jul 2020, 17:17

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved