c++ - cast a pointer to member function in derived class to a pointer to abstract member function -


i'm trying seems should common i've been unable find discussing it. this post on stackoverflow similar i'm trying do, not quite same.

i have abstract base class:

#ifndef _abaseclass_h_ #define _abaseclass_h_  using namespace std;  #include <iostream>  #define call_mbr_func(object, ptr_to_mem_func) ((object).*(ptr_to_mem_func))  class abaseclass { public:    typedef void (abaseclass::*abaseclass_mem_func)();    int a;   int b;    abaseclass();    abaseclass(int a, int b);    virtual void function1(abaseclass_mem_func infunc) = 0;     virtual void function2() = 0;   };  #endif /* _aclass_h_ */ 

and have derived class:

#ifndef _asubclass_h_ #define _asubclass_h_  using namespace std;  #include <iostream> #include "abaseclass.h"   /* simple class containing 2 ints , functions demonstrate passing via various methods.  subclass of aclass*/ class asubclass: public abaseclass { public:    asubclass();    asubclass(int a, int b);    void function1(abaseclass_mem_func infunc);     void function2(void);   };  #endif /* _asubclass_h_ */ 

where function1 , function2 are:

void asubclass::function1(abaseclass_mem_func infunc)  {   call_mbr_func(*this, infunc)(); }   void asubclass::function2(void)  {   = 42;   b = 66; } 

finally, in main() try call function1 targeted on object of type asubclass, passing pointer function2 in asubclass:

int main (int argc, const char * argv[]) {   asubclass eh(2,5); // doesn't work       abaseclass_mem_func trythis = &asubclass::function2; // doesn't work   eh.function1(&asubclass::function2);    return(0); } 

ok, can automatically cast pointer-to-derived type pointer-to-base type. have read can't pass pointer-to-derived-member-function pointer-to-base-member-function. think understand why (the derived member function might make use of things exist in derived class don't exist in base class).

but i'm trying build library of 2 categories of classes (derived 2 base classes). call them baseclass1 , baseclass2. 1 of member functions in derived class baseclass1 needs able handed particular member function derived class baseclass2. there trick can use carry out necessary cast? have use explicit keyword , define cast somehow?

you shorten example lot:

struct b {     virtual void foo() = 0; };  struct d : b {     void foo() override { } };  int main() {     void (b::*ptr)() = &d::foo; // error: cannot initialize variable of                                 // type 'void (b::*)()' rvalue of type                                  // 'void (d::*)()': different classes ('b' vs 'd') } 

the error message, @ least on clang, pretty clear. gcc says cannot initialize. issue cannot implicitly convert pointer-to-derived-member pointer-to-base-member. can explicitly static_cast:

void (b::*ptr)() =      static_cast<void (b::*)()>(&d::foo); // ok! 

side-note: please remove call_mbr_func macro code , never write such thing ever again.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -