【发布时间】:2011-08-18 14:02:16
【问题描述】:
我对指针和引用有一个特定的问题,包括 std::vector 和 std::string。一些问题在以下代码中被剪断,一些在下面。
我基本上有这个代码
//first of all: is this function declaration good?
//or shoudl I already pass the std::vector in another way?
//I'm only reading from it
void func(const std::vector<std::string>& vec)
{
//I need to call otherFunc(void*) with a single element from vec:
//otherFunc(void* arg);
//with arg being a void* to vec[0]
}
我的 IDE 告诉我只有 &*vec[0] 可以作为 otherFunc 的参数,但这不能编译...
如何进行此类参数传递的最佳方式是?
【问题讨论】:
-
你需要解释你想要传递给函数的内容。 “void *”本身是没有意义的。要使用“void *”,您必须先将其转换为其他类型。那么,“void *”的作用是什么?
-
我不知道,因为它来自外部库。我不能改
otherFunc(void*),只能改我自己的func()。
标签: c++ string pointers vector reference