Pointer to SubObject Member



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


  • Moderators

    @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?


  • Qt Champions 2017

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



  • @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();
    }
    


  • @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/... :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.