멤버 함수에 대한 함수 포인터 동일한 클래스의 다른

동일한 클래스의 다른 함수에 대한 포인터 인 클래스의 멤버로 함수 포인터를 설정하고 싶습니다. 내가 이것을하는 이유는 복잡합니다.

이 예에서는 출력이 “1”이되기를 원합니다.

class A {
public:
 int f();
 int (*x)();
}

int A::f() {
 return 1;
}


int main() {
 A a;
 a.x = a.f;
 printf("%d\n",a.x())
}

그러나 이것은 컴파일에 실패합니다. 왜?



답변

구문이 잘못되었습니다. 멤버 포인터는 일반 포인터와 다른 유형 범주입니다. 멤버 포인터는 해당 클래스의 객체와 함께 사용해야합니다.

class A {
public:
 int f();
 int (A::*x)(); // <- declare by saying what class it is a pointer to
};

int A::f() {
 return 1;
}


int main() {
 A a;
 a.x = &A::f; // use the :: syntax
 printf("%d\n",(a.*(a.x))()); // use together with an object of its class
}

a.x함수가 호출되는 객체에 대해서는 아직 말하지 않습니다. 단지 객체에 저장된 포인터를 사용하고 싶다는 의미 a입니다. 붙이는 a받는 왼쪽 피연산자로 다른 시간을 .*조작하는 것은에서 함수를 호출하는 어떤 개체의 컴파일러를 알려줍니다.


답변

int (*x)()멤버 함수에 대한 포인터가 아닙니다. 멤버 함수에 대한 포인터는 다음과 같이 작성 int (A::*x)(void) = &A::f;됩니다..


답변

문자열 명령에서 멤버 함수 호출

#include <iostream>
#include <string>


class A
{
public:
    void call();
private:
    void printH();
    void command(std::string a, std::string b, void (A::*func)());
};

void A::printH()
{
    std::cout<< "H\n";
}

void A::call()
{
    command("a","a", &A::printH);
}

void A::command(std::string a, std::string b, void (A::*func)())
{
    if(a == b)
    {
        (this->*func)();
    }
}

int main()
{
    A a;
    a.call();
    return 0;
}

(this->*func)();클래스 이름으로 함수 포인터를 선언하는 방법에 주의하십시오.void (A::*func)()


답변

함수에 대한 포인터가 아니라 멤버 함수에 대한 포인터를 사용해야합니다.

class A {
    int f() { return 1; }
public:
    int (A::*x)();

    A() : x(&A::f) {}
};

int main() {
   A a;
   std::cout << (a.*a.x)();
   return 0;
}


답변

이것은이 페이지의 다른 곳에서 훌륭한 답변을 기반으로하지만 완전히 해결되지 않은 사용 사례가있었습니다. 함수에 대한 포인터 벡터의 경우 다음을 수행하십시오.

#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

class A{
public:
  typedef vector<int> (A::*AFunc)(int I1,int I2);
  vector<AFunc> FuncList;
  inline int Subtract(int I1,int I2){return I1-I2;};
  inline int Add(int I1,int I2){return I1+I2;};
  ...
  void Populate();
  void ExecuteAll();
};

void A::Populate(){
    FuncList.push_back(&A::Subtract);
    FuncList.push_back(&A::Add);
    ...
}

void A::ExecuteAll(){
  int In1=1,In2=2,Out=0;
  for(size_t FuncId=0;FuncId<FuncList.size();FuncId++){
    Out=(this->*FuncList[FuncId])(In1,In2);
    printf("Function %ld output %d\n",FuncId,Out);
  }
}

int main(){
  A Demo;
  Demo.Populate();
  Demo.ExecuteAll();
  return 0;
}

이와 같은 것은 매개 변수 구문 및 도움말 팁 등과 결합해야하는 색인 ​​기능이있는 명령 인터프리터를 작성하는 경우에 유용합니다. 메뉴에서도 유용 할 수 있습니다.


답변

안타깝게도 기존 멤버 함수 포인터를 일반 함수 포인터로 변환 할 수는 없지만 다음과 같은 일반 함수에서 컴파일 타임에 알려진 멤버 함수 포인터를 래핑하는 매우 간단한 방법으로 어댑터 함수 템플릿을 만들 수 있습니다.

template <class Type>
struct member_function;

template <class Type, class Ret, class... Args>
struct member_function<Ret(Type::*)(Args...)>
{
    template <Ret(Type::*Func)(Args...)>
    static Ret adapter(Type &obj, Args&&... args)
    {
        return (obj.*Func)(std::forward<Args>(args)...);
    }
};

template <class Type, class Ret, class... Args>
struct member_function<Ret(Type::*)(Args...) const>
{
    template <Ret(Type::*Func)(Args...) const>
    static Ret adapter(const Type &obj, Args&&... args)
    {
        return (obj.*Func)(std::forward<Args>(args)...);
    }
};

 

int (*func)(A&) = &member_function<decltype(&A::f)>::adapter<&A::f>;

멤버 함수를 호출하려면의 인스턴스를 A제공해야합니다.


답변

@ IllidanS4의 답변을 기반으로 사전 정의 된 인수와 클래스 인스턴스가있는 거의 모든 멤버 함수를 나중에 호출하기 위해 참조로 전달할 수있는 템플릿 클래스를 만들었습니다.



template<class RET, class... RArgs> class Callback_t {
public:
    virtual RET call(RArgs&&... rargs) = 0;
    //virtual RET call() = 0;
};

template<class T, class RET, class... RArgs> class CallbackCalltimeArgs : public Callback_t<RET, RArgs...> {
public:
    T * owner;
    RET(T::*x)(RArgs...);
    RET call(RArgs&&... rargs) {
        return (*owner.*(x))(std::forward<RArgs>(rargs)...);
    };
    CallbackCalltimeArgs(T* t, RET(T::*x)(RArgs...)) : owner(t), x(x) {}
};

template<class T, class RET, class... Args> class CallbackCreattimeArgs : public Callback_t<RET> {
public:
    T* owner;
    RET(T::*x)(Args...);
    RET call() {
        return (*owner.*(x))(std::get<Args&&>(args)...);
    };
    std::tuple<Args&&...> args;
    CallbackCreattimeArgs(T* t, RET(T::*x)(Args...), Args&&... args) : owner(t), x(x),
        args(std::tuple<Args&&...>(std::forward<Args>(args)...)) {}
};

테스트 / 예 :

class container {
public:
    static void printFrom(container* c) { c->print(); };
    container(int data) : data(data) {};
    ~container() {};
    void print() { printf("%d\n", data); };
    void printTo(FILE* f) { fprintf(f, "%d\n", data); };
    void printWith(int arg) { printf("%d:%d\n", data, arg); };
private:
    int data;
};

int main() {
    container c1(1), c2(20);
    CallbackCreattimeArgs<container, void> f1(&c1, &container::print);
    Callback_t<void>* fp1 = &f1;
    fp1->call();//1
    CallbackCreattimeArgs<container, void, FILE*> f2(&c2, &container::printTo, stdout);
    Callback_t<void>* fp2 = &f2;
    fp2->call();//20
    CallbackCalltimeArgs<container, void, int> f3(&c2, &container::printWith);
    Callback_t<void, int>* fp3 = &f3;
    fp3->call(15);//20:15
}

분명히 이것은 주어진 인수와 소유자 클래스가 여전히 유효한 경우에만 작동합니다. 가독성에 관해서는 … 용서 해주세요.

편집 : 튜플을 일반 저장소로 만들어 불필요한 malloc을 제거했습니다. 참조를 위해 상속 된 유형을 추가했습니다. 대신 호출 시간에 모든 인수를 제공하는 옵션을 추가했습니다. 이제 둘 다 가지고 작업 중입니다 ….

편집 2 : 약속대로 둘 다. 유일한 제한 사항은 미리 정의 된 인수가 콜백 함수에서 런타임 제공 인수 앞에 와야한다는 것입니다. gcc 준수에 대한 도움을 주신 @Chipster에게 감사드립니다. 이것은 우분투의 gcc와 Windows의 Visual Studio에서 작동합니다.

#ifdef _WIN32
#define wintypename typename
#else
#define wintypename
#endif

template<class RET, class... RArgs> class Callback_t {
public:
    virtual RET call(RArgs... rargs) = 0;
    virtual ~Callback_t() = default;
};

template<class RET, class... RArgs> class CallbackFactory {
private:
    template<class T, class... CArgs> class Callback : public Callback_t<RET, RArgs...> {
    private:
        T * owner;
        RET(T::*x)(CArgs..., RArgs...);
        std::tuple<CArgs...> cargs;
        RET call(RArgs... rargs) {
            return (*owner.*(x))(std::get<CArgs>(cargs)..., rargs...);
        };
    public:
        Callback(T* t, RET(T::*x)(CArgs..., RArgs...), CArgs... pda);
        ~Callback() {};
    };
public:
    template<class U, class... CArgs> static Callback_t<RET, RArgs...>* make(U* owner, CArgs... cargs, RET(U::*func)(CArgs..., RArgs...));
};
template<class RET2, class... RArgs2> template<class T2, class... CArgs2> CallbackFactory<RET2, RArgs2...>::Callback<T2, CArgs2...>::Callback(T2* t, RET2(T2::*x)(CArgs2..., RArgs2...), CArgs2... pda) : x(x), owner(t), cargs(std::forward<CArgs2>(pda)...) {}
template<class RET, class... RArgs> template<class U, class... CArgs> Callback_t<RET, RArgs...>* CallbackFactory<RET, RArgs...>::make(U* owner, CArgs... cargs, RET(U::*func)(CArgs..., RArgs...)) {
    return new wintypename CallbackFactory<RET, RArgs...>::Callback<U, CArgs...>(owner, func, std::forward<CArgs>(cargs)...);
}