有没有办法摆脱虚拟成员函数的常量性
我需要为接口实现一个模拟,其定义如下:
class Foo
{
public:
void sendEvent(int id) const = 0;
}
我的模拟类需要保存发送到该类的所有事件 ID。这就是我打算这样做的方式。
class FooMock : Foo
{
private:
m_vector std::vector<int>;
public:
void sendEvent(int id) const {m_vector.push_back(id);}
}
但显然编译器拒绝这种构造。有没有解决方案(假设接口无法更改)?
我意识到我可以为此使用两个类。但是有没有一种方法可以关闭编译器并允许我这样做,类似于 const_cast ?
I need to implement a mock for an interface that is defined something like:
class Foo
{
public:
void sendEvent(int id) const = 0;
}
My mock class needs to save all event id's sent to the class. This is how I intended to do it.
class FooMock : Foo
{
private:
m_vector std::vector<int>;
public:
void sendEvent(int id) const {m_vector.push_back(id);}
}
But obviously the compiler refuses that construction. Are there any solutions to this (assuming the interface could not be changed)?
I realize that I can use two classes for this. But isn't there a way to shut the compiler up and allow me to to this, similar to const_cast?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使向量
可变
,以便可以从 const 方法中修改它,如下所示:但请注意,这使得向量在所有方法中都可变。如果您只想从单个方法写入它,则
const_cast
的侵入性较小,因为您只需一次调用即可将this
的常量转换掉:I' m 这里有点迂腐 - 在 const 方法中,
this
的类型为T const * const
(因此所指向的对象以及指针本身都是 const )。const_cast
只是放弃对象的常量,而不是指针的常量。You can make the vector
mutable
so that it can be modified from within const methods, like this:Note however that this makes the vector mutable from all methods. If you only want to write to it from a single method, a
const_cast
is less invasive, in that you cast the constness ofthis
away just for a single call:I'm being a bit pedantic here - inside a const method,
this
has the typeT const * const
(so both the object being pointed to as well as the pointer itself are const). Theconst_cast
just casts away the constness of the object, but not of the pointer.另一种没有 mutable(当它不可用时)和 const_cast 的方法是使用指针成员。被指点者不遵循常量。
如果可能的话,我会使用 mutable 进行模拟。
Another method without mutable (when that is not available) and const_cast is using pointer-members. The pointees don't follow the constness.
When possible, I would use mutable for mocking.
成员函数的 const 性是函数签名的一部分。你无法摆脱它。
但是,您可以将成员定义为
可变
,您希望在 const 成员函数中改变该成员。即使在 const 成员函数中并且即使对象是 const,关键字mutable
也会使成员可变/可修改。const-ness of a member-function is a part of the function signature. You cannot get rid of it.
However, you can define the member as
mutable
, which you want to mutate in a const member function. The keywordmutable
would make the member mutable/modifiable even in a const-member function and even if the object is const.您可以将
m_vector
标记为mutable
:mutable std::vector; m_vector;
mutable
产生了一些类似于 const_cast 的争议,并导致了一些关于它到底是什么的理论讨论。 > 表示是const
。基本上,只要外部行为保持 const 状,这里就是合理的,我认为在这个例子中这是正确的。You could mark
m_vector
asmutable
:mutable std::vector<int> m_vector;
mutable
generates a bit of controversy similar toconst_cast
, and it results in a bit of a theoretical discussion about what it really means to beconst
. Basically it would be justified here as long as the external behavior remains const-like, which I assume is true looking at this example.