J. *_*Doe 3 c++ const stdvector
为C库编写C++包装器.在库中有一个接受缓冲区的函数unsigned char *.自从使用C++以来,我将我的真实缓冲区存储在std::vector<char>我也添加的包装器类中const,因为所讨论的缓冲区是一个输入缓冲区而且它不会被修改.但是我的代码没有编译,演示:
#include <stdio.h>
#include <vector>
void x(unsigned char *s)
{
printf("%s\n", s);
}
int main()
{
const std::vector<char> s(10);
x(reinterpret_cast<unsigned char *>(s.data())); // FAILS
}
Run Code Online (Sandbox Code Playgroud)
id有什么问题?如果我删除consts从const std::vector<char> s(10);它的工作原理,但我会喜欢你这样.