First Class Functions

Function Pointers and Static Member Functions

Can I take a function pointer to a static member function in C++? How is it different from taking a pointer to a non-static member function?

Abstract art representing computer programming

Yes, you can take a function pointer to a static member function in C++. The syntax is similar to taking a pointer to a non-static member function, but with a few key differences.

Here's an example:

#include <iostream>

class MyClass {
 public:
  static void StaticFunc() {
    std::cout << "Static function called\n";
  }

  void NonStaticFunc() {
    std::cout << "Non-static function called\n";
  }
};

int main() {
  void (*StaticFuncPtr)() =
    &MyClass::StaticFunc;                 
  void (MyClass::*NonStaticFuncPtr)() =
    &MyClass::NonStaticFunc;  

  StaticFuncPtr();  

  MyClass obj;
  (obj.*NonStaticFuncPtr)();  
}
Static function called
Non-static function called

Key points:

  1. For static member functions, the function pointer type does not include the class scope MyClass::. It's just void (*)(), same as a non-member function.
  2. For non-static member functions, the function pointer type includes the class scope MyClass::, like void (MyClass::*)(). This is because non-static member functions have an implicit this parameter, so the class type is part of the function type.
  3. When calling a static member function through a pointer, you call it like a regular function, without an object instance.
  4. When calling a non-static member function through a pointer, you need an object instance, and you use the .* or >* operator to call the function on that instance.

This difference arises because static member functions do not have access to an object instance (there's no this pointer). They operate at the class level, not the object level. Non-static member functions, on the other hand, are always associated with an object instance.

Understanding these differences is important when using function pointers with class member functions. If you try to call a non-static member function pointer without an object instance, or if you try to call a static member function pointer with an object instance, you'll get a compilation error.

This Question is from the Lesson:

First Class Functions

Learn about first-class functions in C++: a feature that lets you store functions in variables, pass them to other functions, and return them, opening up new design possibilities

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

First Class Functions

Learn about first-class functions in C++: a feature that lets you store functions in variables, pass them to other functions, and return them, opening up new design possibilities

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved