Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typedef T Type::* syntax

Tags:

c++

syntax

I need help to understand the following (simplified) code snipped:

template < typename R >
struct Variable
{
    typedef VariableBaseTable< R >  BaseType;
    typedef R BaseType::*            VarType; //!

    template < typename Type >
    R & operator()(Type * obj) const
    {
        return (reinterpret_cast< ObjType >(obj))->*(_variable); //!
    }
    // more stuff follows
}

The two lines I need help understanding are marked with a //! in the end.

I could not understand what the R BaseType::* is defining. In particular, how the ::* syntax works.

In the second marked line, I could not understand the syntax obj->*(_var), I can see that obj is being dereferenced, but what the * is doing there right after it?

like image 591
Kira Avatar asked Feb 11 '26 01:02

Kira


1 Answers

typedef R BaseType::* VarType;

is a type alias which defines VarType as a pointer-to-member-of-BaseType of type R. In the second marked line, you are accessing the field _variable via the pointer-to-member access operator ->*, see e.g. this for more details about the syntax and some examples.

like image 134
vsoftco Avatar answered Feb 13 '26 16:02

vsoftco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!