Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. call parent function from child member
Forum Updated to NodeBB v4.3 + New Features

call parent function from child member

Scheduled Pinned Locked Moved Solved C++ Gurus
11 Posts 4 Posters 3.0k 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.
  • mzimmersM mzimmers

    Hi all -

    Is it possible/legal to invoke a function in a parent class from a method in a member class (NOT a derived class)? This is what I'm trying to do:

    class Child : public QObject {
        void setParentInt() { parent()->setInt(55); }  // <== ERROR HERE
    };
    class Parent : public QObject {
        Child child;
        int m_int;
    public:
        void setInt(int i) { m_int = i; }
    };
    

    Am I just forming the code incorrectly, or is this something fundamentally wrong?

    Thanks...

    JoeCFDJ Offline
    JoeCFDJ Offline
    JoeCFD
    wrote on last edited by
    #2

    @mzimmers if Child has an instance or a pointer of Parent, you can. You have to set it. What is the error message?

    mzimmersM 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      Is it possible/legal to invoke a function in a parent class from a method in a member class (NOT a derived class)? This is what I'm trying to do:

      class Child : public QObject {
          void setParentInt() { parent()->setInt(55); }  // <== ERROR HERE
      };
      class Parent : public QObject {
          Child child;
          int m_int;
      public:
          void setInt(int i) { m_int = i; }
      };
      

      Am I just forming the code incorrectly, or is this something fundamentally wrong?

      Thanks...

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #3

      @mzimmers
      Wot? :)

      Assuming you are intending to use QObject parentage. Right??

      In Child:

      void setParentInt(){ qobject_cast<Parent *>(parent())->setInt(55); } 
      

      In Parent:

      Child child(this);  // Or child.setParent(this);
      

      And before anyone else says, what you are trying to do is likely to be sinful. You probably aren't a parent with a child, because if you were you would know that you would never allow your offspring to directly access anything in yourself (e.g. parent->takeMoneyFrom())....

      mzimmersM JoeCFDJ 2 Replies Last reply
      0
      • JonBJ JonB

        @mzimmers
        Wot? :)

        Assuming you are intending to use QObject parentage. Right??

        In Child:

        void setParentInt(){ qobject_cast<Parent *>(parent())->setInt(55); } 
        

        In Parent:

        Child child(this);  // Or child.setParent(this);
        

        And before anyone else says, what you are trying to do is likely to be sinful. You probably aren't a parent with a child, because if you were you would know that you would never allow your offspring to directly access anything in yourself (e.g. parent->takeMoneyFrom())....

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #4

        @JonB said in call parent function from child member:

        And before anyone else says, what you are trying to do is likely to be sinful....

        Yeah, I kind of figured I was trying to perform an unnatural act here.

        In my real application, I'm trying to incorporate a JSON parser class that I wrote into my various data models. With the parser object, I need to change values of members in my model(s). That's what I was trying to accomplish here.

        JonBJ 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          @mzimmers if Child has an instance or a pointer of Parent, you can. You have to set it. What is the error message?

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #5

          @JoeCFD (I edited the code above) the error is "no member named 'setInt' in 'QObject'."

          I think I'm taking a bad approach to this...

          1 Reply Last reply
          0
          • mzimmersM mzimmers

            @JonB said in call parent function from child member:

            And before anyone else says, what you are trying to do is likely to be sinful....

            Yeah, I kind of figured I was trying to perform an unnatural act here.

            In my real application, I'm trying to incorporate a JSON parser class that I wrote into my various data models. With the parser object, I need to change values of members in my model(s). That's what I was trying to accomplish here.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #6

            @mzimmers
            The code shows how you would do your access to parent. It's the same principle even if you have some other kind of parentage than QObject::parent(), that's not the point though, same would apply for any classes you choose. But it's not a good idea. Low-level child objects (like your JSON parser) should not be going up the tree and changing higher-level things (like a model).

            One general possibility Qt offers is to have the child object emit a signal which the parent chooses to slot onto and adjust its things accordingly. Doesn't mean that's necessarily best here though.

            You should look at rearchitecting :)

            mzimmersM 1 Reply Last reply
            2
            • JonBJ JonB

              @mzimmers
              The code shows how you would do your access to parent. It's the same principle even if you have some other kind of parentage than QObject::parent(), that's not the point though, same would apply for any classes you choose. But it's not a good idea. Low-level child objects (like your JSON parser) should not be going up the tree and changing higher-level things (like a model).

              One general possibility Qt offers is to have the child object emit a signal which the parent chooses to slot onto and adjust its things accordingly. Doesn't mean that's necessarily best here though.

              You should look at rearchitecting :)

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #7

              @JonB OK, that's what I wanted to know. Thanks for the ratification.

              1 Reply Last reply
              0
              • mzimmersM mzimmers has marked this topic as solved on
              • JonBJ JonB

                @mzimmers
                Wot? :)

                Assuming you are intending to use QObject parentage. Right??

                In Child:

                void setParentInt(){ qobject_cast<Parent *>(parent())->setInt(55); } 
                

                In Parent:

                Child child(this);  // Or child.setParent(this);
                

                And before anyone else says, what you are trying to do is likely to be sinful. You probably aren't a parent with a child, because if you were you would know that you would never allow your offspring to directly access anything in yourself (e.g. parent->takeMoneyFrom())....

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by JoeCFD
                #8

                @JonB

                void setParentInt(){ 
                    if ( nullptr != qobject_cast<Parent *>(parent()) ) { /* better check first */
                         qobject_cast<Parent *>(parent())->setInt(55);
                    }
                 } 
                
                JonBJ 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @JonB

                  void setParentInt(){ 
                      if ( nullptr != qobject_cast<Parent *>(parent()) ) { /* better check first */
                           qobject_cast<Parent *>(parent())->setInt(55);
                      }
                   } 
                  
                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #9

                  @JoeCFD
                  I deliberately chose to let it "crash" if parent() is nullptr or not a Parent *, as that corresponds to the original "spec" and what the OP wrote. Of course any error checking desired can be added.

                  JoeCFDJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @JoeCFD
                    I deliberately chose to let it "crash" if parent() is nullptr or not a Parent *, as that corresponds to the original "spec" and what the OP wrote. Of course any error checking desired can be added.

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #10

                    @JonB OK. Better to have some log info here.

                    1 Reply Last reply
                    0
                    • mzimmersM mzimmers

                      Hi all -

                      Is it possible/legal to invoke a function in a parent class from a method in a member class (NOT a derived class)? This is what I'm trying to do:

                      class Child : public QObject {
                          void setParentInt() { parent()->setInt(55); }  // <== ERROR HERE
                      };
                      class Parent : public QObject {
                          Child child;
                          int m_int;
                      public:
                          void setInt(int i) { m_int = i; }
                      };
                      

                      Am I just forming the code incorrectly, or is this something fundamentally wrong?

                      Thanks...

                      Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on last edited by
                      #11

                      @mzimmers

                      Unless I'm misreading the thread, there is a fundemental difference between a class trait object and an inherited class. you can't get "parent" of a trait object and expect it to be the class where the trait is instantiated.

                      When using pure inheritence, you can refer to parent(), but you better use the correct type cast operation when doing so.

                      IOW, parent or superclass access works for is_a, but not for has_a.

                      1 Reply Last reply
                      0

                      • Login

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