简体   繁体   English

C ++,指针,引用,从一个函数传递到另一个函数,std :: vector,std :: string

[英]c++, pointers, references, passing from one to another function, std::vector, std::string

i have a specific problem with pointers and references, with std::vector and std::string. 我在使用std :: vector和std :: string的指针和引用中有一个特定的问题。 Some question are in the following code snipped, some below. 以下代码段中有一些问题,以下是一些问题。

I have basically this code 我基本上有这个代码

//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]
}

My IDE tells me that only &*vec[0] works as parameter for otherFunc , but this doesn't compile... 我的IDE告诉我,只有&*vec[0]用作otherFunc参数,但这不能编译...

How is the best way to do these kind of parameter passing? 如何进行此类参数传递的最佳方法?

That is a good declaration, as long as the function is not intended to modify the vector. 只要函数不打算修改向量,那是一个很好的声明。 It's more efficient than passing by value, since it avoids copying the vector - an expensive operation requiring a memory allocation. 它比按值传递更有效,因为它避免了复制向量-这是需要分配内存的昂贵操作。

However, the other function requires a non-const pointer. 但是,另一个函数需要非常量指针。 How to handle this depends on whether it might modify the data. 如何处理此问题取决于它是否可能修改数据。

If it won't (as you imply when you say "I'm only reading from it") then the options are: 如果不会(如您所暗示的那样,当您说“我只是从中读取”)时,这些选项为:

  • Change it to otherFunc(void const * arg) to give a stronger guarantee that it won't, or 将其更改为otherFunc(void const * arg)可以更otherFunc(void const * arg)保证它不会,或者
  • Remove the const qualification with a const_cast<void*> when calling it 调用时使用const_cast<void*>删除const限定符

Note that &*vec[0] won't compile; 注意&*vec[0]不会编译; you want vec[0].c_str() to get a C-compatible pointer to the first string's data, assuming that's what you need. 您希望vec[0].c_str()获得指向第一个字符串数据的C兼容指针, vec[0].c_str()是您需vec[0].c_str()

If it might modify the vector, then you'll have to do something else, since there's no legal way to modify a std::string through a pointer to its data. 如果它可能会修改向量,那么您将不得不做其他事情,因为没有合法的方法可以通过指向其数据的指针来修改std::string Probably the best option is to use std::vector<char> rather than std::string , but that depends on exactly what the function does. 最好的选择可能是使用std::vector<char>而不是std::string ,但这取决于函数的功能。

Firstly, &*vec[0] is meaningless; 首先, &*vec[0]是没有意义的; you probably mean &vec[0] (ie the address of the first string in your vector). 您可能的意思是&vec[0] (即向量中第一个字符串的地址)。

But even this won't compile because &vec[0] is of type const std::string * . 但是即使这样也不会编译,因为&vec[0]的类型为const std::string * Most importantly, it's const . 最重要的是,它是const You could do this: 您可以这样做:

otherFunc(const_cast<std::string *>(&vec[0]));

BUT!!! 但!!! Trying to use a const std::string * in the context of a void * sounds like a very bad idea. void *的上下文中尝试使用const std::string * void *听起来是一个非常糟糕的主意。 How could that possibly ever do anything useful? 那怎么可能有用呢?

First of all, since you only read from the vector, passing it as a const reference is a good idea and should work well. 首先,由于您仅从向量中读取数据,因此将其作为const引用传递是一个好主意,并且应该可以正常工作。

To your second question: it is difficult to say what otherFunc will do to your object, since it expects a void pointer. 关于第二个问题:很难说otherFunc将对您的对象做什么,因为它期望一个void指针。 This is C style and should not be used in C++, there are better and type safe ways to do so. 这是C样式,不应在C ++中使用,这样做有更好的类型安全方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM