How can you access the enclosing class’s "this" pointer, from a nested union’s method, without passing it via an argument in C++?

  c++, inheritance, qt, this, unions

Basic Idea:

class Card : public QObject
{
        Q_OBJECT
public:
        /* Location of Card */
        union Location
        {
                friend class Card;
                void set( Card         &built    );
                void set( CardList     &list     );
                void set( CardMultiMap &multiMap );
        protected:
                void unbind() // Needs access to 'this' pointer of Card 
 { /* Because it will search for itself in a container and remove any instance of it. */ }; 
        private:
                Card         *m_Built;
                CardList     *m_List;
                CardMultiMap *m_MultiMap;
        } u_Location;
};

I want to do this without having to pass the argument, because it muddies up the code and I am looking for a challenge. I obviously can’t store it as a member variable either. And making an instance inside the enclosing class, needs to be static, which will not work either given their are multiple instances of Card.

So how can I access the this pointer of the Card instance in a nested union method? Unlikely, but if there is a Qt way of doing this, that works too.

Source: Windows Questions C++

LEAVE A COMMENT