Last active
January 4, 2017 19:47
-
-
Save Bueddl/09e324bcdce43c5e53b5ab4ec38b1ff8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class my_class | |
{ | |
public: | |
void setter(int a) | |
{ | |
_some_member = a; | |
} | |
int getter() const | |
{ | |
return _some_member; | |
} | |
private: | |
int _some_member = 42; | |
}; | |
using setter_fn_t = void (void *_this, int a); | |
using getter_fn_t = int (const void *_this); | |
class replacement | |
{ | |
public: | |
void setter(int a) | |
{ | |
_set(reinterpret_cast<void*>(this), a); | |
} | |
int getter() const | |
{ | |
return _get(reinterpret_cast<const void*>(this)); | |
} | |
private: | |
static constexpr setter_fn_t *_set = reinterpret_cast<setter_fn_t*>(&my_class::setter); | |
static constexpr getter_fn_t *_get = reinterpret_cast<getter_fn_t*>(&my_class::getter); | |
}; | |
int main() | |
{ | |
my_class instance; | |
replacement *rep = reinterpret_cast<replacement*>(&instance); | |
std::cout << rep->getter() << std::endl; | |
rep->setter(1337); | |
std::cout << rep->getter() << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment