Returning a Pointer to a Member Function

Returning a Pointer to a Member Function

Let’s say I have a class foo, with a lot of functions that take no arguments and return SomeType:

SomeType foo::bar()SomeType foo::quux()SomeType foo::quuux()SomeType foo::quuuux()

Now I have a pointer to this type of funct :

SomeType (foo::*pfn)();

Now comes the tough part, I want to return it:

?????? SomeFnct(){   SomeType (foo::*pfn)();   pfn = foo::quux;   return pfn;}

What do I write instead of the ??????

Pointers to members can baffle even experienced C++ programmers. There are, however, some syntactic clues that can be helpful. First, let's look at an example that returns a pointer to an ordinary function:

int (*ipifd(int))(double){/*..*/}

The above code snippet declares a function named ipifd that takes double and returns a pointer to a function that takes int and returns int.

Pointers to member functions are very similar to ordinary pointers to function, except that they have an additional qualified name before the asterisk. Thus, for a class named C, the following declaration:

int (C::*ipmifd(int))(double);

declares a function ipmifd that takes a double and returns a pointer to a member function of class C which takes int and returns int.

Now back to your class. A function that takes no argument and returns a member to function of foo, which in turn takes no arguments and returns int, looks like this:

int (foo::*ipifi())(){/*..*/}

This is cumbersome and error prone. In general, you should avoid using such constructs directly because other programmers will have a really hard time understanding what this declaration means. To improve code readability, I recommend using a tydepef to hide the ugly syntax:

typedef int (foo::*PFN)();

Now, defining a function that returns a PFN is trivial:

PFN func(){/*..*/} 

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing