有没有办法摆脱虚拟成员函数的常量性

发布于 2024-12-05 00:17:12 字数 420 浏览 6 评论 0原文

我需要为接口实现一个模拟,其定义如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

满身野味 2024-12-12 00:17:12

您可以使向量可变,以便可以从 const 方法中修改它,如下所示:

mutable std::vector<int> m_vector;

但请注意,这使得向量在所有方法中都可变。如果您只想从单个方法写入它,则 const_cast 的侵入性较小,因为您只需一次调用即可将 this 的常量转换掉:

FooMock * const that = const_cast<FooMock * const>(this);
that->m_vector.push_back(id);

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:

mutable std::vector<int> m_vector;

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 of this away just for a single call:

FooMock * const that = const_cast<FooMock * const>(this);
that->m_vector.push_back(id);

I'm being a bit pedantic here - inside a const method, this has the type T const * const (so both the object being pointed to as well as the pointer itself are const). The const_cast just casts away the constness of the object, but not of the pointer.

风启觞 2024-12-12 00:17:12

另一种没有 mutable(当它不可用时)和 const_cast 的方法是使用指针成员。被指点者不遵循常量。

class FooMock : Foo
{
private: 
  boost::scoped_ptr<std::vector<int> > m_vector;
public:
  FooMock() : m_vector(new std::vector<int>) { }
  void sendEvent(int id) const {m_vector->push_back(id);}
}

如果可能的话,我会使用 mutable 进行模拟。

Another method without mutable (when that is not available) and const_cast is using pointer-members. The pointees don't follow the constness.

class FooMock : Foo
{
private: 
  boost::scoped_ptr<std::vector<int> > m_vector;
public:
  FooMock() : m_vector(new std::vector<int>) { }
  void sendEvent(int id) const {m_vector->push_back(id);}
}

When possible, I would use mutable for mocking.

末蓝 2024-12-12 00:17:12

成员函数的 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 keyword mutable would make the member mutable/modifiable even in a const-member function and even if the object is const.

恬淡成诗 2024-12-12 00:17:12

您可以将 m_vector 标记为 mutable

mutable std::vector; m_vector;

mutable 产生了一些类似于 const_cast 的争议,并导致了一些关于它到底是什么的理论讨论。 > 表示是 const。基本上,只要外部行为保持 const 状,这里就是合理的,我认为在这个例子中这是正确的。

You could mark m_vector as mutable:

mutable std::vector<int> m_vector;

mutable generates a bit of controversy similar to const_cast, and it results in a bit of a theoretical discussion about what it really means to be const. Basically it would be justified here as long as the external behavior remains const-like, which I assume is true looking at this example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文