我有一个方法接受void*&
作为参数,我想通过this
作为参数。将其转换为void *&
例子:
struct A
{
void foo()
{
bar((void*&)this);
}
private:
void bar(void*& p) {}
};
我有以下的编译器错误:
cast_this.cpp: In member function 'void A::foo()':
cast_this.cpp:5:14: error: invalid cast of an rvalue expression of type 'A*' to type 'void*&'
bar((void*&)this);
^
有没有什么办法来施放this
指针?
编辑: 与bar((void* const &)this);
试图给出:
cast_this.cpp: In member function 'void A::foo()':
cast_this.cpp:5:25: error: binding 'void* const' to reference of type 'void*&' discards qualifiers
bar((void* const &)this);
^
cast_this.cpp:8:10: note: initializing argument 1 of 'void A::bar(void*&)'
void bar(void*& p) {}
^
C++达到合理长度以保证您的安全。你需要一个像@Piotr这样的表达来提供自己在脚下的射击。但要明白,你会*用这样的代码在脚下自我射击。 'bar'有一个签名,意味着它改变了'void *',但是你不能改变'this'。 – MSalters