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. Pointer to SubObject Member
Forum Updated to NodeBB v4.3 + New Features

Pointer to SubObject Member

Scheduled Pinned Locked Moved Unsolved C++ Gurus
5 Posts 4 Posters 1.2k 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.
  • J Offline
    J Offline
    Josz
    wrote on last edited by
    #1

    hello,
    A simple question; I have an object and a subobject, in myObject I know how to make a member function pointer to its owns methods, like I did it below

    class MySubObject {
     static const  int i = 0;
    public:
            void show(int add) {
                qDebug() << i +add << endl;
             }
    };
    
    class MyObject {
      mySubObject mSO;
    public:
    
    void print() {  qDebug() << "Im your object" << endl;  }
    };
    
    void (MyObject::*pointerToPrint)() = &MyObject::print; //<- I make here a function pointer to own methrod
    
    
    

    The question is, How could I create afunction pointer to void show(int add) of MySubObject into MyObject?

    Any help will be wellcommed.
    Thanks in advance.

    raven-worxR kshegunovK 2 Replies Last reply
    0
    • J Josz

      hello,
      A simple question; I have an object and a subobject, in myObject I know how to make a member function pointer to its owns methods, like I did it below

      class MySubObject {
       static const  int i = 0;
      public:
              void show(int add) {
                  qDebug() << i +add << endl;
               }
      };
      
      class MyObject {
        mySubObject mSO;
      public:
      
      void print() {  qDebug() << "Im your object" << endl;  }
      };
      
      void (MyObject::*pointerToPrint)() = &MyObject::print; //<- I make here a function pointer to own methrod
      
      
      

      The question is, How could I create afunction pointer to void show(int add) of MySubObject into MyObject?

      Any help will be wellcommed.
      Thanks in advance.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Josz
      a function pointer is bound to a class? there is no hierarchy involved. To call the method you need an object instance unless, it's a static method.
      Or do i misunderstand something here?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • J Josz

        hello,
        A simple question; I have an object and a subobject, in myObject I know how to make a member function pointer to its owns methods, like I did it below

        class MySubObject {
         static const  int i = 0;
        public:
                void show(int add) {
                    qDebug() << i +add << endl;
                 }
        };
        
        class MyObject {
          mySubObject mSO;
        public:
        
        void print() {  qDebug() << "Im your object" << endl;  }
        };
        
        void (MyObject::*pointerToPrint)() = &MyObject::print; //<- I make here a function pointer to own methrod
        
        
        

        The question is, How could I create afunction pointer to void show(int add) of MySubObject into MyObject?

        Any help will be wellcommed.
        Thanks in advance.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @Josz said in Pointer to SubObject Member:

        The question is, How could I create afunction pointer to void show(int add) of MySubObject into MyObject?

        The same way:

        typedef void (MySubObject::*MethodWithIntArgument)(int);
        MethodWithIntArgument pointerToShow = &MySubObject::show;
        

        As @raven-worx said, there's nothing really special about pointers-to-member; they just hold the address of the method that's located in the binary's stub. Basically all methods are regular functions that get this injected into them when they're called. This is also the reason you can take an address to a method (or a member variable) - you are basically telling the compiler 'give me the address (offset) of the entry'.
        Now, when you call such a thing it requires a bound object that's going to be the this pointer. That's why you get the weird call:

        MySubObject myObject;
        (myObject.*pointerToShow)(25);
        

        Virtual methods are somewhat different as there's (yet) another level of indirection to them that is the vtable of the class, and that's referenced by the vptr of the actual (polymorphic) object. Sufficed to say they work the same way from the user's point of view - i.e. polymorphic behavior is going to be exhibited regardless of the call being made directly or through a function pointer.

        Read and abide by the Qt Code of Conduct

        J 1 Reply Last reply
        3
        • kshegunovK kshegunov

          @Josz said in Pointer to SubObject Member:

          The question is, How could I create afunction pointer to void show(int add) of MySubObject into MyObject?

          The same way:

          typedef void (MySubObject::*MethodWithIntArgument)(int);
          MethodWithIntArgument pointerToShow = &MySubObject::show;
          

          As @raven-worx said, there's nothing really special about pointers-to-member; they just hold the address of the method that's located in the binary's stub. Basically all methods are regular functions that get this injected into them when they're called. This is also the reason you can take an address to a method (or a member variable) - you are basically telling the compiler 'give me the address (offset) of the entry'.
          Now, when you call such a thing it requires a bound object that's going to be the this pointer. That's why you get the weird call:

          MySubObject myObject;
          (myObject.*pointerToShow)(25);
          

          Virtual methods are somewhat different as there's (yet) another level of indirection to them that is the vtable of the class, and that's referenced by the vptr of the actual (polymorphic) object. Sufficed to say they work the same way from the user's point of view - i.e. polymorphic behavior is going to be exhibited regardless of the call being made directly or through a function pointer.

          J Offline
          J Offline
          Josz
          wrote on last edited by
          #4

          @kshegunov, @raven-worx Thank you.

          I can't reach it;

          I only try to add a method to myObject that behaves like a symbolic link to the subObject method and so, don't jump twice from method to method when I call the method in the parent.

          please, if my idea is possible, could you me show how?

          Thanks in advance.

          Here is my attempt (Is perhaps not possible??)

          #include <QCoreApplication>
          #include <iostream>
          
          using namespace std;
          
          class MySubObject {
          public:
                 void print(){ //With static don't work
                  cout << "I'm your SubObject" << endl;
              }
          };
          typedef void (MySubObject::*dPSO)();
          
          class MyObject {
          public:
               MySubObject mS;
               
              void print(){
                  cout << "I'm your object" << endl;
              }
          };
          
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
          
              MyObject mO;
              //(mO.mS.*pSOprint)(); //don't work
              m0.(*pSOprint)();  //dont work, I would like to make anything so
              return a.exec();
          }
          
          JonBJ 1 Reply Last reply
          0
          • J Josz

            @kshegunov, @raven-worx Thank you.

            I can't reach it;

            I only try to add a method to myObject that behaves like a symbolic link to the subObject method and so, don't jump twice from method to method when I call the method in the parent.

            please, if my idea is possible, could you me show how?

            Thanks in advance.

            Here is my attempt (Is perhaps not possible??)

            #include <QCoreApplication>
            #include <iostream>
            
            using namespace std;
            
            class MySubObject {
            public:
                   void print(){ //With static don't work
                    cout << "I'm your SubObject" << endl;
                }
            };
            typedef void (MySubObject::*dPSO)();
            
            class MyObject {
            public:
                 MySubObject mS;
                 
                void print(){
                    cout << "I'm your object" << endl;
                }
            };
            
            
            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
            
                MyObject mO;
                //(mO.mS.*pSOprint)(); //don't work
                m0.(*pSOprint)();  //dont work, I would like to make anything so
                return a.exec();
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Josz

            //(mO.mS.*pSOprint)(); //don't work
            

            Where is your MySubObject::pSOprint defined and initialized to MySubObject::print(), if that's what you're trying to do?

             m0.(*pSOprint)();  //dont work, I would like to make anything so
            

            Similarly for MyObject::pSOprint? [And btw that m0 should be mO.]

            In your original you have

            void (MyObject::*pointerToPrint)() = &MyObject::print; //<- I make here a function pointer to own methrod
            

            Where are you doing the equivalent of this for the function you want to access in your MySubObject? I'm not C++, but don't you need something like one of (hopefully the first):

            void (MyObject::*pSOPrint)() = &MySubObject::print;
            void (MyObject::*pSOPrint)() = &MyObject::mS::print;
            

            Also, when you say "don't work" for things, it would save a lot of guessing if you said it doesn't compile/crashes/doesn't do what you want/... :)

            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