簡體   English   中英

指向C ++中的函數對象

[英]pointer to function object in C++

我想將一個函數對象傳遞給一個類,該類將使用函數對象在類中做一些工作。

但問題是,我不會傳入函數對象。所以我想,在類中定義一個void *指針,這個指針將用傳入的函數對象初始化。

代碼如下:

class A 
{
    public:
        //...
        void doSomeJob(int x)
        {
            (*functor)(x); //use the function object to process data x
        }

    private:
        //...
        void *functor; //function object's pointer will be assigned to this pointer
};

但代碼不起作用。 我想,不能用那種方式使用void *functor

我知道我可以使用template class來完成這項工作,但我的問題是,我仍然可以使用pointer to function object完成工作嗎?

PS

為了使我的問題更清楚,可能有幾個函數對象因處理數據而彼此不同,我不會傳遞什么函數對象,但我知道每個函數對象都將采用一個int參數。

正如一些答案所說,我可以通過function pointer完成工作,但是函數對象比函數指針有更多的實用工具,比如states ,這就是我要用的東西。

如果呼叫機構的類型未存儲在呼叫機構可訪問的某個位置,則無法在呼叫站點調用未知類型的功能對象。

有兩種選擇:

如果你可以使用C ++ 11或boost,你可以使用std::function resp。 boost::function

class A
{
public:
  // ...
  void doSomeJob(int x)
  {
    functor(x);
  }
private:
  std::function<void(int)> functor; // or boost::function when using boost
};

這里的類型(以隱式形式)存儲在function模板的機制中。

否則,如果您可以要求傳遞的所有函數對象都具有從特定基類派生的類類型,則可以創建抽象基類:

struct AbstractFunctor
{
  virtual void operator()(int) = 0;
};

class A
{
public:
  // ...
  void doSomeJob(int x)
  {
    (*functor)(x);
  }
private:
  AbstractFunctor* functor; // or boost::function when using boost
};

這里的類型存儲在函數對象的虛擬表中。

如果你真的不能使用boost,你也可以自己編寫類似的解決方案。 關鍵詞是“類型擦除”,它基本上通過動態生成來自已知基類的派生對象(如我的第二個解決方案),它知道對象的類型並可以調用它。 它可能大致如下(未經測試的代碼):

class int_function
{
private:
  struct abstract_forward
  {
    virtual void call(int) = 0;
    virtual abstract_forward clone() const = 0;
    virtual ~abstract_forward() {}
  };
  template<typename Functor> struct forward: abstract_forward
  {
    forward(Functor f): func(f) {}
    void call(int i) { func(i); }
    abstract_forward clone() const { return new forward<Functor>(func); }
    Functor func;
  };
public:
  template<typename Functor> int_function(Functor f)
  {
    forwarder = new forward<Functor>(f);
  }
  int_function(int_function const& other)
  {
    forwarder = other.forwarder->clone();
  }
  int_function& operator=(int_function const& other)
  {
    abstract_forward* newfwd = other.forwarder->clone();
    delete forwarder;
    forwarder = newfwd;
  }
  ~int_function()
  {
    delete forwarder}
  }
  void operator()(int i)
  {
    forwarder->call(i);
  }
private:
  abstract_forward* forwarder;
};

class A
{
public:
  void doSomeJob(int x)
  {
    functor(x);
  }
private:
  int_function functor;
};
void *functor; //function object's pointer will be assigned to this pointer

這不是函數指針。

你需要的是這個:

void (*functor)(int); //function pointer

更好的是(在C ++ 11中):

#include <functional> //must include this 

std::function<void(int)> functor;

//then use it as:
functor(x);  //not (*functor)(x)

正確的語法是:

void (*functor)(int);

有關聲明和使用函數指針的更多信息,請參閱本教程: http//www.cprogramming.com/tutorial/function-pointers.html

C ++對其類型非常嚴格,因此您不能只使用void*作為函數指針。 指針必須是一個實際的函數指針,供您調用。

你是什​​么意思,你不知道將傳遞什么功能對象? 在該示例中,您知道它接受一個intint&作為參數,並且可能返回void ,例如,因此您可以將函數指針存儲為:

void (*func)(int);

如果你的意思是說你想要能夠存儲類成員函數,或者重載operator()的類的實例,那么如果你有C,你可以使用std::functionstd::bind from <functional> ++ 11,或boost::functionboost::bind

boost::function<void (int)> func;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM