C++使用另一个类的成员函数进行模板赋值的指针成员函数
class IShaderParam{
public:
std::string name_value;
};
template<class TParam>
class TShaderParam:public IShaderParam{
public:
void (TShaderParam::*send_to_shader)( const TParam&,const std::string&);
TShaderParam():send_to_shader(NULL){}
TParam value;
void up_to_shader();
};
typedef TShaderParam<float> FloatShaderParam;
typedef TShaderParam<D3DXVECTOR3> Vec3ShaderParam;
在另一个类中,我有一个 IShaderParams* 向量和我想发送到“send_to_shader”的函数。我尝试像这样分配这些函数的引用:
Vec3ShaderParam *_param = new Vec3ShaderParam;
_param->send_to_shader = &TShader::setVector3;
这是函数:
void TShader::setVector3(const D3DXVECTOR3 &vec, const std::string &name){
//...
}
这是带有 IshaderParams* 的类:
class TShader{
std::vector<IShaderParam*> params;
public:
Shader effect;
std::string technique_name;
TShader(std::string& afilename):effect(NULL){};
~TShader();
void setVector3(const D3DXVECTOR3 &vec, const std::string &name);
当我使用 Visual Studio C++ Express 2008 编译项目时,我收到此错误:
错误 2 错误 C2440:“=”:无法生成 转换 'void (__thiscall TShader::* )(const D3DXVECTOR3 &,const std::string &)' a 'void (__thiscall TShaderParam::* )(const TParam &,const std::string &)' c:\users\isagoras\documents\mcv\afoc\shader.cpp 127
我可以做作业吗?不?我不知道怎么办:-S 是的,我知道我可以使用其他技术实现相同的目标,但我想知道我该如何做到这一点..
class IShaderParam{
public:
std::string name_value;
};
template<class TParam>
class TShaderParam:public IShaderParam{
public:
void (TShaderParam::*send_to_shader)( const TParam&,const std::string&);
TShaderParam():send_to_shader(NULL){}
TParam value;
void up_to_shader();
};
typedef TShaderParam<float> FloatShaderParam;
typedef TShaderParam<D3DXVECTOR3> Vec3ShaderParam;
In another class, I have a vector of IShaderParams* and functions that i want to send to "send_to_shader". I'm trying assign the reference of these functions like this:
Vec3ShaderParam *_param = new Vec3ShaderParam;
_param->send_to_shader = &TShader::setVector3;
This is the function:
void TShader::setVector3(const D3DXVECTOR3 &vec, const std::string &name){
//...
}
And this is the class with IshaderParams*:
class TShader{
std::vector<IShaderParam*> params;
public:
Shader effect;
std::string technique_name;
TShader(std::string& afilename):effect(NULL){};
~TShader();
void setVector3(const D3DXVECTOR3 &vec, const std::string &name);
When I compile the project with Visual Studio C++ Express 2008 I recieve this error:
Error 2 error C2440: '=' :can't make
the conversion 'void (__thiscall
TShader::* )(const D3DXVECTOR3 &,const
std::string &)' a 'void (__thiscall
TShaderParam::* )(const TParam
&,const std::string &)'
c:\users\isagoras\documents\mcv\afoc\shader.cpp
127
Can I do the assignment? No? I don't know how :-S
Yes, I know that I can achieve the same objective with other techniques, but I want to know how can I do this..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那是因为您试图使用不兼容的指针。
setVector3
是一个TShader
方法,但send_to_shader
需要一个TShaderParam
方法。不确切知道你想要什么,你可以通过将以下内容更改
为:来编译你的示例:
That's because you are trying to use incompatible pointers.
setVector3
is aTShader
method butsend_to_shader
wants aTShaderParam
method.Not knowing exactly what you want, you can get your example to compile by changing this:
to this:
大概
TShader::send_to_shader
是一个 C 风格的函数指针?成员函数不能用作函数指针回调 -它们需要一个
this
参数。静态成员函数和全局函数可以用作函数指针。
因此,您可以
手动将
this
作为参数传递给静态函数回调,该回调又调用相应实例上的成员函数使用接口而不是函数
使用 < a href="http://en.wikipedia.org/wiki/Function_object#In_C_and_C.2B.2B" rel="nofollow noreferrer">函子
所有这三个都需要轻微的架构更改。
Presumably
TShader::send_to_shader
is a c-style function pointer?Member functions cannot be used as function pointer callbacks - they require a
this
parameter.Static member functions and global functions can be used as function pointers.
So you can either
pass the
this
manually as a parameter to a static function callback, which in turn invokes the member function on the appropriate instanceuse an interface instead of a function
use a functor
All three require slight architectural changes.
指向成员函数的指针有点僵化。如果您向我们展示生成错误的代码行将会有所帮助,但只需查看它,
所以你已经传递了一个
void (TShader::* )(const D3DXVECTOR3 &,const std::string &)
但有人期望一个void (TShaderParam: :* )(const TParam &,const std::string &)
。您需要将
setVector3
重新实现为从TShaderParam
派生的类型的方法,并从第一个参数中提取D3DXVECTOR3 &
,该参数必须是TParam
。 (但如果它只是传回您自己的值,您可以将其static_cast
为您自己的从TParam
派生的类型。)希望这会有所帮助!
Pointers to member functions are a bit rigid. It would help if you showed us the line of code that generates the error, but just looking at it,
So you've passed a
void (TShader::* )(const D3DXVECTOR3 &,const std::string &)
but someone expects avoid (TShaderParam::* )(const TParam &,const std::string &)
.You need to reimplement
setVector3
as a method of a type derived fromTShaderParam
and extract yourD3DXVECTOR3 &
from the first argument, which must be aTParam
. (But if it's just passing back your own value, you canstatic_cast
it to your own type derived fromTParam
.)Hope this helps!