为什么不能在memcpy中使用const参数? [英] why cant I use const arguments in memcpy?
本文介绍了为什么不能在memcpy中使用const参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 struct cVector3d
,我将 memcpy
嵌入到如下的字符数组中:
I have a struct cVector3d
and I am memcpy
'ing it into a char array like this:
void insert_into_stream(std::ostream& stream, cVector3d vector)
{
int length = sizeof(double)*3;
char insert_buffer[sizeof(double)*3];
memcpy(insert_buffer, &vector[0], length);
stream.write(insert_buffer, length);
}
如果我使用 const cVector3d向量
在参数列表中,比我得到一个& requires l-value
错误。
If I use const cVector3d vector
in the parameter list, than I get a "& requires l-value
" error.
推荐答案
原因是您链接的文档:
double & operator[] (unsigned int index)
double operator[] (unsigned int index) const
当你使用非const版本,你得到一个l值引用,你可以取其地址(这是引用 double
的地址)。当你使用const-version时,你会得到一个临时的语言,并禁止你使用它的地址。
When you use the non-const version you get a l-value reference and you can take its address (which is the address of the referenced double
). When you use the const-version, you get a temporary and the language forbids you to take its address.
这篇关于为什么不能在memcpy中使用const参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文