[英]Can't assign a member which is a pointer to a templatized class
My problem is that in my "Widget" class i've the following declaration: 我的问题是在“ Widget”类中,我有以下声明:
MouseEvent* X;
In a member function I initialize the pointer with an address the normal way: 在成员函数中,我以正常方式用地址初始化指针:
X = new MouseEvent;
Ok, this last line makes the compiler stop at: 好的,这最后一行使编译器停止在:
error C2166: l-value specifies const object 错误C2166:左值指定const对象
All right, a MouseEvent is declared as a typedef to simplify things: 好的,将MouseEvent声明为typedef以简化操作:
typedef Event__2<void, Widget&, const MouseEventArgs&> MouseEvent;
And Event__2 is, as you may imagine as: (basic structure shown): 如您所想,Event__2是:(显示的基本结构):
template <typename return_type, typename arg1_T, typename arg2_T>
class Event__2
{
...
};
I don't know where the Event__2 class gets the const qualifier. 我不知道Event__2类在哪里获取const限定词。 Any tips ? 有小费吗 ?
Thanks. 谢谢。
Likely, the member function where you are initializing X is marked as const - something like this. 可能将初始化X的成员函数标记为const-类似这样。
class Foo
{
int *Bar;
public:
void AssignAndDoStuff() const
{
Bar = new int; // Can't assign to a const object.
// other code
}
}
The solution here is either to 解决方案是
mutable
. 将Bar标记为mutable
。 Pick one of the above: 选择上述之一:
class Foo
{
mutable int *Bar; // 3
public:
void Assign() // 1
{
Bar = new int;
}
void DoStuff() const
{
// Other code
}
void AssignAndDoStuff() // 2
{
Bar = new int;
// other code
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.