Artyer
Artyer

Reputation: 40801

How is a pointer to a member function different than a pointer to a data member?

So I have this alias template:

template<class MemberT, class ClassT>
using make_member_ptr = MemberT ClassT::*;

And I noticed that make_member_ptr<int(char, long), class_type> is the same that int(class_type::*)(char, long). Before, I was thinking that a pointer to a member function is completely different to a pointer to a data member.

How do pointers to member functions differ from pointers to data members?

The only thing that I could find is that if the member function is virtual, calling through a pointer to the base function will call the derived function, which is a non-issue for pointers a non-function member.

The reason I am asking is that I am dealing with generic pointers to members, and I want to know what I have to look out for and special case for pointers to member functions vs data members.

Upvotes: 2

Views: 184

Answers (1)

eerorika
eerorika

Reputation: 238321

Before, I was thinking that a pointer to a member function is completely different to a pointer to a data member.

You've thought correctly.

How do pointers to member functions differ from pointers to data members?

They are separate types. They may have different sizes. Pointer to a member function can point to a member function. Pointer to a data member can point to a data member. The difference is analogous to the one between function pointers and data pointers.

make_member_ptr<int(char, long), class_type> is a pointer to member function.

Upvotes: 1

Related Questions