0

I Recently came across this c++ weired syntax used with function and compiled successfully. I could not able to make sense out of it , what this really does.

How "*member" does not gave me undefined error or something else, because its not declared anywhere else.

Can anyone let me how to call this function?

template<class POS, class META>
size_t test11( META POS:: *member)
{
    /********/
} 

Thanks everyone.

RaHuL
  • 101
  • 1
  • 12
  • 1
    wrt/ why there is no undefined error: `member` is the name of the variable being declared. `META POS::*` is the type. It's a pointer to member. See https://en.cppreference.com/w/cpp/language/pointer#Pointers_to_data_members –  Dec 10 '21 at 13:32
  • 2
    `member` is a pointer to a member of class `POS` and has type `META` – Alan Birtles Dec 10 '21 at 13:34
  • 1
    [Demo](https://wandbox.org/permlink/BPL90nSN4YUODMCU). – dfrib Dec 10 '21 at 13:36
  • 1
    [another demo](https://godbolt.org/z/MvM9zrTbT) - passing a member function – Ted Lyngmo Dec 10 '21 at 13:40

1 Answers1

0

When you wrote

META POS:: *member

this means that member is a pointer to a member of class POS that has type META.

Now coming to your second question,

Can anyone let me how to call this function?

One possible example is given below:

#include <iostream>
#include <string>
struct Name
{
    std::string name;
    
    Name() = default;
    Name(std::string pname): name(pname)
    {
        std::cout<<"Constructor called"<<std::endl;
    }
};

template<class POS, class META>
size_t test11( META POS:: *member)
{
    //create object of type Name
    Name n("Anoop");
    
    //create a pointer to object n
    Name *ptrN = &n;
    
    //Now let's use the variable member 
    std::cout<<ptrN->*member<<std::endl; 
    
    return 5;//don't forget to return something since the function has non-void return type
}
int main()
{
    //create a pointer to non-static member
    std::string Name::*ptrData = &Name::name; 
    
    //pass the pointer ptrData to the template function test11<>
    test11(ptrData);
    
    
}

The output of the program is:

Constructor called
Anoop

which can be verified here.

Explanation

When i wrote

test11(ptrData);

in the above code sample, then using template argument deduction, POS is deduced to be the type Name and META is deduced to be the type std::string.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • Thanks everyone for your time in answering. @Anoop Rana I have one last doubt so if we create a pointer to member then that member becomes a part of class forever ? – RaHuL Dec 10 '21 at 16:07
  • @RaHuL You're welcome. In your last comment you asked **if we create a pointer to member then that member becomes a part of class forever ?** . The member(both data member and member function) is already a part of the class that is why we call them a member of the class. Or Did you mean that the **pointer becomes a part of the class**. Can you clarify your doubt(that you wrote in your last comment) so that i can explain exactly what you're looking for because currently by looking at your comment I could not understand what your doubt is. – Jason Dec 10 '21 at 16:17
  • thanks Anoop. Yes exactly my doubt is whether pointer becomes part of class? – RaHuL Dec 10 '21 at 16:24
  • 1
    @RaHuL Okay, **No** pointer doesn't become part of the class. It is a separate object. It's just that it can point to a member of the specified class. And by pointing to the member, the pointer itself **does not** become part of the class. You're welcome. – Jason Dec 10 '21 at 16:31