-5

I try to understand somecode, which could be summarized into something like this:

class FooClass {
    public:
        void Foo();
        static void (FooClass::*Foo_Original)();
};

void (FooClass::* FooClass::Foo_Original)() = 0;
void FooClass::Foo() {
    (this->*Foo_Original)();
}

This is part of a more complex dll solution in Visual Studio. From debugger I found that method Foo() is called from some other dll. Can someone explain what this syntax means? What is it supposed to do?

That's not duplicate to this one. C++: Pointer to class data member Please, be more thoughtful

Community
  • 1
  • 1
user3237732
  • 1,976
  • 2
  • 21
  • 28

2 Answers2

2

FooClass exposes a member function Foo() and a static pointer to a member function. The pointer is called Foo_Original.

As it is a static pointer, it must be initialised, here with 0 (aka nullptr).

The function Foo() just calls the function that is pointed to by Foo_Original. Of course, this will do other thing as U.B, only if the pointer was initialized somewhere to a member function.

Example:

class FooClass {
public:
    void Foo();
    static void (FooClass::*Foo_Original)();
    // additional member functions for the demo:
    void Bar()  { std::cout << "Bar was called" << std::endl; }
    void Goo()  { std::cout << "Goo was called" << std::endl; }
};
int main()
{
    FooClass f; 
    // f.Foo(); ==> U.B, as Foo_Original is still 0 
    f.Foo_Original = &FooClass::Bar;
    f.Foo(); 
    f.Foo_Original = &FooClass::Goo;
    f.Foo();
}

P.S.: I don't know how this relates to dll and dll injection in your specific case, but I could imagine that your FooClass loads dynamically some DLLs and offers a standardized interface for them.

Christophe
  • 68,716
  • 7
  • 72
  • 138
0

I don't use them much, but this looks like it's declaring a ptr-to-a-FooClass-member-function that takes zero args and 'returns' void. I'm not sure if it's declaring a static pointer though, or a pointer-to-a-static-member-function).

  • Your hesitation is understandable (and this alone would justify an upvote of the original question) ! It's a static pointer to a non static member function. There would be no `FooClass::*` for a static function. [This FAQ](https://isocpp.org/wiki/faq/pointers-to-members) should clarify your doubt :-) – Christophe Aug 15 '15 at 14:54