对函数指针的 const 引用

发布于 2024-11-18 01:16:32 字数 1003 浏览 2 评论 0原文

我有一个函数 Foo 和一个类 CDelegate。

typedef void (*typeFctPtr)(void*);


void Foo(void* dummy)
{
  cout << "Foo\n";
}


class CDelegate 
{ 
  public:

  CDelegate (const typeFctPtr& f_ref_Wrapper, void* f_pvSubscriber) 
    : m_ref_Wrapper(f_ref_Wrapper), m_pvSubscriber(f_pvSubscriber) 
  {
  }

  inline void operator () () const
  {
    (*m_ref_Wrapper)(0);
  }

  inline void operator=(const CInterruptDelegate& D)
  {
  }

private:
  void* m_pvSubscriber;
  const typeFctPtr& m_ref_Wrapper;
};

第二个类有一个静态成员 static CDelegate m_Delegate;,我使用构造函数初始化它,如下所示:

CInterruptDelegate CSpi1::m_Delegate(FreeFunction, 0);

我想通过调用静态对象的 () 运算符来调用 FooCSpi1::m_Delegate();

我在 (*m_ref_Wrapper)(0); 处遇到异常 语法有问题吗?我不太确定我尝试做的事情是否可行。我有一个可行的解决方案,其中 CDelegate 的构造函数不采用函数指针的 const 引用,而是采用函数指针本身。然后我可以毫无问题地调用 () 运算符中的函数。我想使用 const 引用,因为函数指针调用无法优化,我希望通过 const 引用的调用可以,因为一切都应该在编译时知道。

I have a function Foo and a class CDelegate.

typedef void (*typeFctPtr)(void*);


void Foo(void* dummy)
{
  cout << "Foo\n";
}


class CDelegate 
{ 
  public:

  CDelegate (const typeFctPtr& f_ref_Wrapper, void* f_pvSubscriber) 
    : m_ref_Wrapper(f_ref_Wrapper), m_pvSubscriber(f_pvSubscriber) 
  {
  }

  inline void operator () () const
  {
    (*m_ref_Wrapper)(0);
  }

  inline void operator=(const CInterruptDelegate& D)
  {
  }

private:
  void* m_pvSubscriber;
  const typeFctPtr& m_ref_Wrapper;
};

A second class has a static member static CDelegate m_Delegate; which I initialize using the constructor like this:

CInterruptDelegate CSpi1::m_Delegate(FreeFunction, 0);

I want to call Foo by calling the ()operator of my static object: CSpi1::m_Delegate();

I get an exception at (*m_ref_Wrapper)(0);
Is there something wrong with the syntax? I am not quite sure if what I try to do is possible at all. I have a working solution where the constructor of CDelegatedoes not take a const reference of a function pointer but the function pointer itself. I can then call the function in the ()operator without problems. I want to use a const reference because the function pointer call cannot be optimized and I hope the call via the const reference can because everything should be known at compile time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半窗疏影 2024-11-25 01:16:32

您持有对函数指针的引用(并且该指针是一个临时指针,在您使用它时已被销毁,因此事情会发生严重错误)。

尝试将您的 typedef 更改为函数类型:

typedef void typeFct(void*);
...
const typeFct & m_ref_Wrapper;

然后使用现有代码,您最终将获得对函数的引用,这样就可以了。或者您可以存储指向函数的指针 - const typeFct *

无论哪种情况,调用都可以是 m_ref_Wrapper(0)

一般来说,我更喜欢 typedef 函数类型,而不是指针或引用,只是因为语法不太难看。

You're holding a reference to a pointer to a function (and the pointer is a temporary which has been destroyed by the time you use it, so things go badly wrong).

Try changing your typedef to be a function type:

typedef void typeFct(void*);
...
const typeFct & m_ref_Wrapper;

Then with your existing code you'll end up with a reference to a function, and you'll be fine. Or you could store a pointer to the function - const typeFct *.

And in either case the call can just be m_ref_Wrapper(0).

In general I prefer to typedef function types rather than pointer-to or reference-to, if only because the syntax is less ugly.

稚然 2024-11-25 01:16:32

我认为问题在于成员的声明:

const typeFctPtr& m_ref_Wrapper;

相反,尝试删除 &

const typeFctPtr m_ref_Wrapper;

引用必须始终引用另一个现有对象。在这种情况下,它将引用创建的临时对象,以在调用构造函数时保存对指针的引用。

当我们这样做时,我建议您也应该从构造函数中删除引用。其原因是使用标量时没有任何增益:s。

另一件可以让你的代码更具可读性的事情是,如果你能 typedef 函数的类型,而不是指向函数的指针。这样,很明显您传递了一个指针。

以下是一个精简版本,总结了我上面建议的更改:

typedef void (typeFct)(void*);

class CDelegate
{
  public:

  CDelegate (typeFct * f_Wrapper, void* f_pvSubscriber)
    : m_ref_Wrapper(f_Wrapper), m_pvSubscriber(f_pvSubscriber) 
  {
  }

  inline void operator () () const
  {
    (*m_ref_Wrapper)(0);
  }

private:
  void* m_pvSubscriber;
  typeFct * m_ref_Wrapper;
};

I believe that the problem is the declaration of the member:

const typeFctPtr& m_ref_Wrapper;

Instead, try to drop the &:

const typeFctPtr m_ref_Wrapper;

A reference must always reference another existing object. In this case, it will refer the temporary object created to hold the reference to the pointer at the time the constructor is called.

While we're at it, I would suggest that you should drop the reference from the constructor as well. The reason for this is that there is no gain when you work with scalar:s.

Another thing that would make your code more readable is if you would typedef the type of the function, rather than the pointer to the function. That way, it would be clear that you pass around a pointer.

The following is a cut-down version that summarized the changes I suggest above:

typedef void (typeFct)(void*);

class CDelegate
{
  public:

  CDelegate (typeFct * f_Wrapper, void* f_pvSubscriber)
    : m_ref_Wrapper(f_Wrapper), m_pvSubscriber(f_pvSubscriber) 
  {
  }

  inline void operator () () const
  {
    (*m_ref_Wrapper)(0);
  }

private:
  void* m_pvSubscriber;
  typeFct * m_ref_Wrapper;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文