0

Does C++ have feature that absorb all functions of another class in some ways?

More specifically, D want to absorb all B's functions that is public and have C* as the first parameter.

#include <iostream>
using namespace std;

class C{public: int va=2;};
class B{
    public:    
    void f1(C* cPtr,int p,float q){  std::cout<<(cPtr->va+p)<<std::endl; }
    int f2(C* cPtr,std::string str){  std::cout<<(cPtr->va)<<std::endl; return cPtr->va+100; }
    //f3,f4, ....  
};
class D{//absorber
    public: static D construct(B* bPtr,C* cPtr){
        ????   
    }
    ?????  
};

int main() {
    C* cPtr=new C();
    B* bPtr=new B();
    D d=D::construct(bPtr,cPtr);
    d.f1(2,3.0);         //will call : bPtr->f1(cPtr,2,3.0)
    int k=d.f2("f2");    //will call : bPtr->f2(cPtr,"f2")
    return 0;
}

I know that I can do it per-function, using <...> or manually.
Can I do it in a more automatic way (one piece of code, absorb all f1,f2,...)?

If I can't, are there any similar features?

I may use it to comfort some calling.
There might be question about this, but I don't know a correct keyword to search for them.

Solution using macro is acceptable, but not much desirable.
A short answer like "No, and there are no similar features." is OK.

javaLover
  • 6,347
  • 2
  • 22
  • 67
  • Why dont you just make B a parent of D? – Vality Mar 07 '17 at 02:07
  • @Vality Do you mean, `D` derived from `B`? I want to call a function in 2 different ways. I don't think it can help. – javaLover Mar 07 '17 at 02:09
  • Oh, sorry. I misunderstood your question. That is a lot more difficult. Look into Boost Fusion maybe. – Vality Mar 07 '17 at 02:11
  • @Vality After some skimming, I don't think [boost fusion (example in SO)](http://stackoverflow.com/a/456184/3577745) can help. It seems to be per-function utility, but thank! – javaLover Mar 07 '17 at 02:15
  • I think what you're asking for requires [Reflection](https://en.wikipedia.org/wiki/Reflection_(computer_programming)) which I don't think really exists in C++. Probably the best you can do is some combination of macros and possibly making use of [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind) – smead Mar 07 '17 at 02:54
  • @smead Thank for the holy keyword! .... I personally don't like `std::bind` - it is [slow](http://stackoverflow.com/q/24852764/3577745). I don't like `macro` either - it tends to cause mild-to-moderate maintainability issue. I can imagine how it can be done with `std::bind` & `macro`. Thank. :) – javaLover Mar 07 '17 at 03:07

0 Answers0