Increment value from void pointer - c++

I'm trying to modify some variables [not necessary from the same class/struct] from keyboard's shortcuts, something like that:
A foo struct containing variables:
struct Foo {
int a;
float b;
};
struct Foo2 {
int c;
};
And a main like:
int main() {
Foo f;
Foo2 f2
void* p = &(f.a); //it could be &(f2.c)
if ('A' key activated) {
*p += 1;
}
}
Currently, I'm stucked at this point:
error: invalid operands to binary expression ('void' and 'int')
The only way to make it work is to change:
*p += 1;
By:
*(int*)p += 1;
Which is not a good solution, because I should not know the type pointed by p. Is there a way to do that?

Converting the pointer to void* lost the type information and the compiler will not know how to increment. Why don't you make a pointer to Foo instead?
int main() {
Foo f;
Foo* p = &f;
if ('A' key activated) {
p->a += 1;
}
}
Also keep in mind that incrementing a float is not a good idea!
For the quesion in the comment of this answer:
struct FooBar
{
int *a;
float *b;
};
int main() {
Foo f;
Bar b;
FooBar fb{&f.a, &b.b};
if ('A' key activated) {
*(fb.a) += 1;
}
}
Note that this solution is rather C-style. Look at lethal-guitar's answer for a more C++-style solution.

Edit: At first I didn't realize that you want to have different types per entry. Based on the task of handling keyboard shortcuts, you could use a polymorphic class, and put instances of it into a std::map:
class KeyHandler {
public:
virtual void onKeyStroke() = 0;
};
class MyHandler : public KeyHandler {
public:
MyHandler(int& value) : myValue(value) {}
virtual void onKeyStroke() {
myValue_ += 1;
}
private:
int& myValue_; // Other subclasses could have other data
};
// Now place instances of different Handlers into a std::map
typedef std::shared_ptr<KeyHandler> PKeyHandler;
std::map<char, PKeyHandler> bindings;
bindings['A'] = PKeyHandler(new IncrementIntHandler(&someInt));
bindings['B'] = PKeyHandler(new IncrementFloatHandler(&someFloat));
// The actual input handler then just invokes
// the correct handler for a key stroke.
bindings[keyCode]->onKeyStroke();
That way, you can define a handler class for every action you want to support, and implement the corresponding logic into these classes. You could make the base class' implementation just do nothing to handle non-mapped keys, etc.

Sure, use an int pointer instead:
int * p = &f.a;
if ( /* condition */ ) { ++*p; }

Related

Can you make a bool return 1 if a variable is of a certain type?

class A {
protected:
int a;
public:
int getA() const
{
return a;
}
};
class B : public A
{
private:
int b;
public:
int getB() const
{
return b;
}
};
class C
{
private:
int c;
A* obj;
public:
C()
{
obj = new A[5];// obj is initialized with some values in the constructor, but i won't do it here
}
void f()
{
c += obj[0].getB();
}
~C()
{
delete obj;
}
};
The problem I am facing right now is that i want the f function to add to the variable c the value of b from the object obj[0] if the type of obj[0] is B. But if the obj[0] is A and not B I dont want anything to happen.
Is there a bool that would be 1 if a certain variable is a certain type?
I know i could overload the f function and make it take a parameter the B obj[0] and another one that takes as a paramter the A obj[0], the last function having an empty body, but i was wondering if there is a more simple/efficient way of doing it.
I have bee asked to provide an example of where I would need this specific solution so here it is
class Item
{
protected:
std::string Name;
unsigned long long Number;
bool Placeable;
};
class Tool : public Item
{
private:
long double AttackDamage;
long double AttackSpeed;
public:
long double getAttackDamage() const
{
return this->AttackDamage;
}
long double getAttackSpeed() const
{
return this->AttackSpeed;
}
};
class Mob
{
protected:
Item* Inventory;
unsigned long long InventorySize;
unsigned long long MainHand;
std::string Name;
long double AttackDamage;
long double AttackSpeed;
public:
Mob(unsigned long long n)
{
this->AttackDamage = 1;
this->AttackSpeed = 0.5;
this->InventorySize = n;
this->Inventory = new Item[this->InventorySize];
for (int i = 0; i < this->InventorySize; ++i)
this->Inventory[i] = e; // e in empty slot, like a 0 initializer for integers
this->MainHand = 0;
}
void setStats()
{
this->AttackDamage += this->Inventory[this->MainHand].getAttackDamage();
this->AttackSpeed += this->Inventory[this->MainHand].getAttackSpeed();
}
~Mob()
{
delete Invenory;
}
};
The method i need help with is void SetStats() in Mob. I want the function to only update the values of AttackDamage and AttackSpeed if the item at MainHand position is a Tool. Otherwise i dont want any updates. I could add stas to the Item class like AttackDamage and AttackSpeed and set them to 0 which would make no issue but if i would be working on a more serious project i would have more stats than AttackDamage and Speed and there would be a lot of unnecesarry memory.
This is just a fraction of the code, like not all variables are initialized and there might be some things i forgot to paste
You need at least one virtual function in the base class, otherwise there is no polymorphism. The canonical way is to define a virtual destructor:
class A {
// ...
virtual ~A() = default;
};
To use polymorphism, you can't have value types. You need pointers or references. So instead of A obj, you'd need to use A* obj. You then try to dynamic_cast obj to a B* pointer. If obj is indeed pointing to a B, the cast succeeds and returns a valid B* pointer you can use. If obj is not pointing to a B, the cast fails and returns a null pointer:
class C {
private:
int c;
A* obj;
public:
void func()
{
if (auto casted_obj = dynamic_cast<B*>(obj)) {
c += casted_obj->getB();
}
}
};

How to define, assign and use an Array of Pointer-to-Method?

In my project I have an hierarchy of classes. An object of type C_t contains (dynamically allocated) array of A_t objects and a B_t object.
I want to invoke an A_t method a_fn() from a B_t's method b_fn(). I can do that in several ways, and several levels of indirection.
B maintains a pointer to its containing object C. Then I use this pointer to directly refer to A[] and its method.
Next, I defined pointers to A_t and initialized them with the references to A[]s, and use these pointers to indirectly invoke A[]'s method.
Assume that in the actual project the hierarchy is deeper and that the object names are descriptive and long, this style becomes long and convoluted statements.
I want to maintain an array of pointers to A[]'s method a_fn() and use these array members to invoke the methods. I.e., how to make the (commented out) statements that would print x = 46 and x = 47 work?
#include <iostream>
using namespace std;
struct A_t;
struct B_t;
struct C_t;
// Top level class
struct C_t {
A_t *A[2];
B_t *B;
C_t();
};
struct A_t {
void a_fn(int x) { cout << "x = " << x << endl; };
};
// Define func-ptr to A_t's method
typedef void (A_t::*p_fn_t)(int);
struct B_t {
C_t* p_C; // ptr to containing object
A_t** p_A[2]; // array of ptrs-to-array-of-As
p_fn_t p_fn; // ptr to A_t's method
p_fn_t pa_fn[2]; // array of ptr to A_t's method
void b_fn() {
p_C->A[0]->a_fn(10); // Cptr-direct
(p_C->A[0]->*p_fn)(11); // Cptr-indirect
(*p_A)[1]->a_fn(22); // Aptr-direct
((*p_A)[1]->*p_fn)(23); // Aptr-indirect
((*p_A)[0]->*(pa_fn[0]))(34); // Aptr-fptr-indirect
((*p_A)[1]->*(pa_fn[1]))(35); // Aptr-fptr-indirect
//pa_fn[0](46); // <<-- HOW TO DO THIS???
//pa_fn[1](47); // <<-- HOW TO DO THIS???
};
B_t(C_t *C) : p_C(C) {
p_fn = &A_t::a_fn; // init fptr-to-A's-method
p_A[0] = &(p_C->A[0]); // init ptr-to-A[0]
p_A[1] = &(p_C->A[1]); // init ptr-to-A[1]
// The following assignments need to change in order to get
// what I am looking for. Something along the line of
// pa_fn[0] = &(A[0]->(A_t::a_fn));
pa_fn[0] = &A_t::a_fn; // init fptr-to-A's-method
pa_fn[1] = &A_t::a_fn; // init fptr-to-A's-method
};
};
C_t::C_t() {
// Instantiate As and B and init B with C's own ptr
A[0] = new A_t;
A[1] = new A_t;
B = new B_t(this);
};
int main(int argc, char *argv[])
{
C_t C;
C.B->b_fn(); // Invoke B's method
return 0;
}
This program's output:
x = 10
x = 11
x = 22
x = 23
x = 34
x = 35
Update: In the actual project there is another level of hierarchy, so A_t contains AA_t and B_t contains BB_t object pointers. So, the method invocation will look like:
((*p_A)[0]->AA->*(pa_fn[0]))(34);
and given long names:
((*p_A_object)[0]->AA_sub_object->*(pa_method_fn[0]))(34);
It is easy to see how this can become long and hard to read.
As Toby pointed out you can't do it with plain pointers, but is possible with
function+bind:
std::function<void(int)> f_fn = std::bind(&A_t::a_fn, p_A[0]) ;
To make this work you need an instance of A_t to call one of its functions, not only a function pointer. E.g. like this:
#include <iostream>
struct foo { void bar(int x) {std::cout << "moo " << x << std::endl; } };
typedef void (foo::*p_fn_t)(int);
void test(foo f,p_fn_t fn){ (f.*fn)(3); }
int main() {
foo f;
p_fn_t fn = &foo::bar;
test(f,fn);
}
The syntax is easy to get wrong. It is .* or ->* to invoke a function via a member function pointer.
PS: it seems like A_ts function in your example could be as well static, which would explain why you didnt realize that you need an instance. In that case you might consider to simply use free functions instead of putting them into a struct. (Actually I dont know how to invoke a static function via a member function pointer)

Object storing a non-owning reference that must be informed before the reference is destructed

I have a class following this pattern:
class Foo
{
public:
// Create a Foo whose value is absolute
Foo(int x) : other_(0), a_(x) {}
// Create a Foo whose value is relative to another Foo
Foo(Foo * other, int dx) : other_(other), a_(dx) {}
// Get the value
double x() const
{
if(other_)
return other_->x() + a_;
else
return a_;
}
private:
Foo * other_;
int a_;
};
The Foo objects are all owned by a Bar:
class Bar
{
public:
~Bar() { for(int i=0; i<foos_.size(); i++) delete foos_[i]; }
private:
vector<Foo*> foos_;
};
Of course, this is a simplified example to get the idea. I have a guarantee that there are no loop of Foos, and that linked Foos all belong to the same instance of Bar. So far, so good. To do things the C++11 way, I would use vector< unique_ptr<Foo> > foos_; in Bar, and pass foos_[i].get() as potential argument of a Foo constructor.
There is the deal:
This a GUI application, and the user can interactively delete some Foo at will. The expected behaviour is that if foo1 is deleted, and foo2 is relative to foo1, then foo2 becomes now "absolute":
void Foo::convertToAbsolute() { a_ += other_->x(); other_ = 0; }
void usageScenario()
{
Foo * foo1 = new Foo(42);
Foo * foo2 = new Foo(foo1, 42);
// Here, foo1->x() = 42 and foo2->x() = 84
foo1->setX(10);
// Here, foo1->x() = 10 and foo2->x() = 52
delete foo1;
// Here, foo2->x() = 52
}
I know it is possible to do it using raw pointers, by using a a DAG structure with back-pointers, so the Foo are aware of who "depends on them", and can inform them before deletion (possible solutions detailed here and here ).
My question is: Would you handle it the same way? Is there a way using standard C++11 smart pointers to avoid having the explicit back-pointers, and then avoid explicitely calling areRelativeToMe_[i]->convertToAbsolute(); in the destructor of Foo? I was thinking about weak_ptr, something in the spirit of:
class Foo { /* ... */ weak_ptr<Foo> other_; };
double Foo::x() const
{
if(other_.isExpired())
convertToAbsolute();
// ...
}
But the issue is that convertToAbsolute() needs the relative Foo to still exist. So I need a non-owning smart-pointer that can tell "this reference is logically expired", but actually extends the lifetime of the referenced object, until it is not needed.
It could be seen either like a weak_ptr extending the lifetime until it is not shared with any other weak_ptr:
class Foo { /* ... */ extended_weak_ptr<Foo> other_; };
double Foo::x() const
{
if(other_.isExpired())
{
convertToAbsolute();
other_.reset(); // now the object is destructed, unless other
// foos still have to release it
}
// ...
}
Or like a shared_ptr with different level of ownership:
class Bar { /* ... */ vector< multilevel_shared_ptr<Foo> foos_; };
class Foo { /* ... */ multilevel_shared_ptr<Foo> other_; };
void Bar::createFoos()
{
// Bar owns the Foo* with the highest level of ownership "Level1"
// Creating an absolute Foo
foos_.push_back( multilevel_unique_ptr<Foo>(new Foo(42), Level1) );
// Creating a relative Foo
foos_.push_back( multilevel_unique_ptr<Foo>(new Foo(foos_[0],7), Level1) );
}
Foo::Foo(const multilevel_unique_ptr<Foo> & other, int dx) :
other_( other, Level2 ),
// Foo owns the Foo* with the lowest level of ownership "Level2"
a_(dx)
{
}
double Foo::x() const
{
if(other_.noLevel1Owner()) // returns true if not shared
// with any Level1 owner
{
convertToAbsolute();
other_.reset(); // now the object is destructed, unless
// shared with other Level2 owners
}
// ...
}
Any thoughts?
All Foo are owned by Bar. Therefore all deletions of Foo happen in Bar methods. So I might implement this logic inside Bar:
void Bar::remove(Foo* f)
{
using namespace std::placeholders;
assert(std::any_of(begin(foos_), end(foos_),
std::bind(std::equal_to<decltype(f)>(), f, _1));
auto const& children = /* some code which determines which other Foo depend on f */;
std::for_each(begin(children), end(children),
std::mem_fn(&Foo::convertToAbsolute));
foos_.remove(f);
delete f; // not needed if using smart ptrs
}
This would ensure that the expiring Foo still exists when convertToAbsolute is called on its dependents.
The choice of how to compute children is up to you. I would probably have each Foo keep track of its own children (cyclic non-owning pointers), but you could also keep track of it inside Bar, or search through foos_ on demand to recompute it when needed.
You can use the double link approach even with more than one other dependent object. You only have to link together the dependents of the same object:
class Foo {
public:
explicit Foo(double x)
: v(x), foot(nullptr), next(nullptr), dept(nullptr) {}
// construct as relative object; complexity O(1)
Foo(Foo*f, double x)
: v(x), foot(f), dept(nullptr)
{ foot->add_dept(this); }
// destruct; complexity O(n_dept) + O(foot->n_dept)
// O(1) if !destroy_carefully
~Foo()
{
if(destroy_carefully) {
for(Foo*p=dept; p;) {
Foo*n=p->next;
p->unroot();
p=n;
}
if(foot) foot->remove_dept(this);
}
}
double x() const
{ return foot? foot->x() + v : v; }
private:
double v; // my position relative to foot if non-null
Foo*foot; // my foot point
Foo*next; // next object with same foot point as me
Foo*dept; // first object with me as foot point
// change to un-rooted; complexity: O(1)
void unroot()
{ v+=foot->x(); foot=nullptr; next=nullptr; }
// add d to the linked list of dependents; complexity O(1)
void add_dept(const Foo*d)
{ d->next=dept; dept=d; }
// remove d from the linked list of dependents ; complexity O(n_dept)
void remove_dept(const Foo*d)
{
for(Foo*p=dept; p; p=p->next)
if(p==d) { p=d->next; break; }
}
static bool destroy_carefully;
};
bool Foo::destroy_carefully = true;
Here, setting Foo::destroy_carefully=false allows you to delete all remaining objects without going through the untangling of mutual references (which can be expensive).
Interesting problem. I guess you figured that you can add a pointer to the 'child' object. I am not sure, whether smart pointers help here. I tried to implement the code below using std::weak_ptr<Foo> but you can only use it for other_ and not for the listener.
Another thought I had was to leave the responsibility to some higher power. The problem that you have is that you would like to do the update when the destructor is called. Perhaps better approach would be to call convertToAbsolute() from somewhere else. For example, if you are storing the Foos in a vector and the user clicks delete in the UI, you need the index of the object in order to delete so might as well update the adjacent item to absolute value.
Below is a solution that uses a Foo*.
#include <iostream>
#include <memory>
#include <vector>
class Foo
{
public:
// Create a Foo whose value is absolute
Foo(int x) : other_(nullptr), listener_(nullptr), a_(x)
{}
// Create a Foo whose value is relative to another Foo
Foo(Foo* other, int dx) :
other_(other), listener_(nullptr), a_(dx)
{
other->setListener(this);
}
~Foo()
{
convertToAbsolute();
if (listener_)
listener_->other_ = nullptr;
}
// Get the value
double x() const
{
if(other_)
return other_->x() + a_;
else
return a_;
}
void setX(int i)
{
a_ = i;
}
void convertToAbsolute()
{
if (listener_)
listener_->a_ += a_;
}
void setListener(Foo* listener)
{
listener_ = listener;
}
private:
Foo* other_;
Foo* listener_;
int a_;
};
void printFoos(const std::vector<std::shared_ptr<Foo>>& foos)
{
std::cout << "Printing foos:\n";
for(const auto& f : foos)
std::cout << '\t' << f->x() << '\n';
}
int main(int argc, const char** argv)
{
std::vector<std::shared_ptr<Foo>> foos;
try
{
auto foo1 = std::make_shared<Foo>(42);
auto foo2 = std::make_shared<Foo>(foo1.get(), 42);
foos.emplace_back(foo1);
foos.emplace_back(foo2);
}
catch (std::exception& e)
{
std::cerr << e.what() << '\n';
}
// Here, foo1->x() = 42 and foo2->x() = 84
printFoos(foos);
foos[0]->setX(10);
// Here, foo1->x() = 10 and foo2->x() = 52
printFoos(foos);
foos.erase(foos.begin());
// Here, foo2->x() = 52
printFoos(foos);
return 0;
}
If you have a Signal/Slot framework, that provides a nice place to do the unlinking. For example, using the Qt libraries these classes could look like:
class Foo : public QObject
{
Q_OBJECT
public:
// Create a Foo whose value is absolute
Foo(int x) : QObject(nullptr), other_(nullptr), a_(x) {}
// Create a Foo whose value is relative to another Foo
Foo(Foo * other, int dx) : QObject(nullptr) other_(other), a_(dx) {
connect(other, SIGNAL(foo_dying()), this, SLOT(make_absolute()));
}
~Foo() { emit foo_dying(); }
// Get the value
double x() const
{
if(other_)
return other_->x() + a_;
else
return a_;
}
signals:
void foo_dying();
private slots:
void make_absolute()
{
a_ += other_->x();
other_ = nullptr;
}
private:
Foo * other_;
int a_;
};
Here is probably the simplest way to achieve the goal using back-pointers. You can use the container you want depending on your complexity requirements (e.g., a set, hash table, vector, linked list, etc.). A more involved but more efficient approach is proposed by Walter.
class Foo
{
public:
// Create a Foo whose value is absolute
Foo(int x) : other_(0), a_(x) {}
// Create a Foo whose value is relative to another Foo
Foo(Foo * other, int dx) : other_(other), a_(dx)
{
other->areRelativeToMe_.insert(this);
}
// Get the value
double x() const
{
if(other_)
return other_->x() + a_;
else
return a_;
}
// delete the Foo
Foo::~Foo()
{
// Inform the one I depend on, if any, that I'm getting destroyed
if(other_)
other_->areRelativeToMe_.remove(this);
// Inform people that depends on me that I'm getting destructed
for(int i=0; i<areRelativeToMe_.size(); i++)
areRelativeToMe_[i]->convertToAbsolute();
}
private:
Foo * other_;
int a_;
Container<Foo*> areRelativeToMe_; // must provide insert(Foo*)
// and remove(Foo*)
// Convert to absolute
void convertToAbsolute()
{
a_ += other_->x();
other_ = 0;
}
};

Complicated test to check which object instantiates a function call

I have a struct ( can be class ) and is defined in another class as shown
struct A{
somedata_A;
somespecificimplementation_A(someclass *S1);
};
class someclass{
somedata_someclass;
A a;
};
main(){
someclass c1, *c2;
c2 = &c1;
c1.a.somespecificimplementation_A(c2);
}
How do I verify that c2 is indeed a reference for c1? Pardon me for putting up this example as it is obvious that c2 is reference for c1.
Update: A does not store a pointer to someclass
If you don't know nothing about parent, compare member' adresses
void A::somespecificimplementation_A(someclass *S1)
{
if (this == &(S1->a)) {
// parent == S1
} else {
// parent != S1
}
}
Like that:
struct A{
int somedata_A;
int somespecificimplementation_A(someclass *S1){
if ((void*) &(S1->a) == this)
{
std::cout << "S1 is a pointer to myself" << std::endl;
return 1;
}
return 0;
}
};
Assuming struct A has a pointer to c1, you can then take a pointer to c2 and compare pointer values? Similar to what you would do with assignment operator overloads?
Why go the way around and pass a pointer of your class to the nested struct which you then have to test, when you can instead give a reference to the parent by the parent during its construction?
class someclass
{
public:
struct a
{
void foo()
{
parent.doSomething();
}
private:
friend someclass;
a(someclass & parent)
: parent(parent)
{}
someclass & parent;
} a;
someclass() : a(*this) {}
private:
void doSomething()
{
}
};
Although technically unspecified, the following will work on
most modern, general purpose machines:
void A::somespecificimplementation_A( someclass* S1 )
{
char const* s = reinterpret_cast<char const*>( S1 );
char const* t = reinterpret_cast<char const*>( this );
if ( this >= s && this < s + sizeof( someclass ) ) {
// This A is a member of S1
} else {
// This A isn't
}
}
Having said that, I would stress:
This is not specified by the standard. It will work on
machines with a flat, linear addressing, but may fail (give
false positives) on a machine with e.g. segmented memory.
I'd seriously question the design if A needs to know who it
is a member of.
And if A really does need this information, it really should store
a pointer to someclass, which is passed in to its constructor, so that the dependency is manifest.

raw function pointer from a bound method

I need to bind a method into a function-callback, except this snippet is not legal as discussed in demote-boostfunction-to-a-plain-function-pointer.
What's the simplest way to get this behavior?
struct C {
void m(int x) {
(void) x;
_asm int 3;
}};
typedef void (*cb_t)(int);
int main() {
C c;
boost::function<void (int x)> cb = boost::bind(&C::m, &c, _1);
cb_t raw_cb = *cb.target<cb_t>(); //null dereference
raw_cb(1);
return 0;
}
You can make your own class to do the same thing as the boost bind function. All the class has to do is accept the function type and a pointer to the object that contains the function. For example, this is a void return and void param delegate:
template<typename owner>
class VoidDelegate : public IDelegate
{
public:
VoidDelegate(void (owner::*aFunc)(void), owner* aOwner)
{
mFunction = aFunc;
mOwner = aOwner;
}
~VoidDelegate(void)
{}
void Invoke(void)
{
if(mFunction != 0)
{
(mOwner->*mFunction)();
}
}
private:
void (owner::*mFunction)(void);
owner* mOwner;
};
Usage:
class C
{
void CallMe(void)
{
std::cout << "called";
}
};
int main(int aArgc, char** aArgv)
{
C c;
VoidDelegate<C> delegate(&C::CallMe, &c);
delegate.Invoke();
}
Now, since VoidDelegate<C> is a type, having a collection of these might not be practical, because what if the list was to contain functions of class B too? It couldn't.
This is where polymorphism comes into play. You can create an interface IDelegate, which has a function Invoke:
class IDelegate
{
virtual ~IDelegate(void) { }
virtual void Invoke(void) = 0;
}
If VoidDelegate<T> implements IDelegate you could have a collection of IDelegates and therefore have callbacks to methods in different class types.
Either you can shove that bound parameter into a global variable and create a static function that can pick up the value and call the function on it, or you're going to have to generate per-instance functions on the fly - this will involve some kind of on the fly code-gen to generate a stub function on the heap that has a static local variable set to the value you want, and then calls the function on it.
The first way is simple and easy to understand, but not at all thread-safe or reentrant. The second version is messy and difficult, but thread-safe and reentrant if done right.
Edit: I just found out that ATL uses the code generation technique to do exactly this - they generate thunks on the fly that set up the this pointer and other data and then jump to the call back function. Here's a CodeProject article that explains how that works and might give you an idea of how to do it yourself. Particularly look at the last sample (Program 77).
Note that since the article was written DEP has come into existance and you'll need to use VirtualAlloc with PAGE_EXECUTE_READWRITE to get a chunk of memory where you can allocate your thunks and execute them.
#include <iostream>
typedef void(*callback_t)(int);
template< typename Class, void (Class::*Method_Pointer)(void) >
void wrapper( int class_pointer )
{
Class * const self = (Class*)(void*)class_pointer;
(self->*Method_Pointer)();
}
class A
{
public:
int m_i;
void callback( )
{ std::cout << "callback: " << m_i << std::endl; }
};
int main()
{
A a = { 10 };
callback_t cb = &wrapper<A,&A::callback>;
cb( (int)(void*)&a);
}
i have it working right now by turning C into a singleton, factoring C::m into C::m_Impl, and declaring static C::m(int) which forwards to the singleton instance. talk about a hack.