Share via

nested class as reference but not a point

josh BAUER 166 Reputation points
2020-11-29T13:47:29.963+00:00

define UpperClass(address, type, field) ((type *)( (PCHAR)(address) - (ULONG_PTR)(&((type *)0)->field)))

class myClass {
    int howToShareIt;
public:
    class subClass {
    public:
        operator int() {
            myClass p = UpperClass(this, myClass, overloadedClass);
            return p.howToShareIt;
        }
        subClass& operator=(int rhs)
        {
            myClass* p = CONTAINING_RECORD(this, myClass, overloadedClass);
            p.howToShareIt = rhs;
            return *this;
        }
    };
    subClass overloadedClass;

};

UpperClass above is CONTAINING_RECORD. I am trying to change it so it treats myClass p as reference
but not a pointer. I would like to use it as reference so I would write
p.howToShareIt rather than p->howToShareIt.
I would not use -> but I would use simple dot sign ".".

How to do this?
Is it possible to treat class by reference but not a pointer?

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

Answer accepted by question author

RLWA32 52,571 Reputation points
2020-11-29T14:05:26.927+00:00

If you want to use a reference instead of a pointer -

        myClass& mcref = *(CONTAINING_RECORD(this, myClass, overloadedClass));

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.