【问题标题】:C++ Passing args to void functionC++ 将 args 传递给 void 函数
【发布时间】:2015-07-15 07:46:24
【问题描述】:

如何将参数传递给这个函数:

int myClass::myFunc(void * aArgs){

    return 0;
}

我这样称呼它:

thrd_create(&t, myClass::myFunc, (void*)0);

我需要向函数传递多个参数,如何实现?

【问题讨论】:

  • 然后创建一个结构体作为参数存储并将这个结构体传递给函数。
  • “我需要向函数传递多个参数,我该如何实现?” 使用struct 并传递那个实例的地址。
  • 如果你有 C++11,你可以选择std::thread,这是标准的,可以很容易地传递任意数量的参数,同时保持常规的函数签名(没有@987654326 @)。
  • @chris 我没有 C++11,使用 Visual Studio 2005 编译器。谢谢。

标签: c++ function void args


【解决方案1】:

有很多方法。例如:

struct Arg_Struct
{
  int m_nArg1;
  float m_fArg2;
  bool m_bArg3;
}
//...
Arg_Struct* pStruct = new Arg_Struct;
pStruct->m_nArg1 = 0;
thrd_create(&t, myClass::myFunc, (void*)pStruct);

//...

int myClass::myFunc(void * aArgs){
   Arg_Struct* pArgs = (Arg_Struct*)aArgs;
   int n = pArgs->m_nArg1;
//...
   return 0;
}

【讨论】:

  • 谢谢!如何在我的函数中获取 args?我从未使用过 struct。
  • 感谢伙伴的帮助,即使很明显我对C一无所知。
猜你喜欢
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多