可以从纯虚函数返回引用吗?

发布于 2024-11-17 03:11:28 字数 226 浏览 2 评论 0原文

class I {
public:
  virtual std::wstring const& GetName() const = 0;
};

通常,实现此接口的客户端将其名称包含在其主体中。一切都很好。但有时'GetName'的结果是在函数执行期间计算的。使用静态变量来存储结果并不是一个好主意。但按值返回需要额外的性能开销。

那么这个问题该如何解决呢?谢谢。

class I {
public:
  virtual std::wstring const& GetName() const = 0;
};

Usually clients which implement this interface contain their name inside their body. And everything works just fine. But sometimes the result of 'GetName' is calculated during function execution. Using static variable to store result is not a very good idea. But returning by value requires additional performance overhead.

So how this problem could be solved? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

じ违心 2024-11-24 03:11:28

但是按值返回需要
额外的性能开销。

这不是真的。编译器通常能够使用返回值优化来消除副本。

But returning by value requires
additional performance overhead.

This is not true. The compiler will very often be able to elide the copy using Return Value Optimization.

暮年慕年 2024-11-24 03:11:28

不要猜测性能问题:虽然人们经常通过 const 引用返回 std::string ,但按值返回确实没什么大不了的,特别是如果您别无选择。

实际上,有时您甚至可以更改后者,因为它只影响客户端代码的一些内容。

请记住:过早的优化是万恶之源。

现在,要完全回答这个问题,您可以完美地返回虚函数中的引用(无论它是否是纯函数并不重要)。只要确保永远不会返回对临时对象的引用即可。

如果某个派生类需要执行成本高昂的计算,它仍然可以将结果存储在成员 std::wstring 中并返回对此的引用。但再次强调,不要猜测性能问题:首先进行测量,只有当这似乎是瓶颈时才关注它。

Don't guess performance issues: while people often return std::string by const reference, it is really not a big deal to return one by value, especially if you have no other choice.

Actually, you can sometimes even change that latter as it affects only a few things for the client code.

Remember: premature optimization is the root of all evil.

Now, to fully answer the question, you can perfectly return a reference in virtual function (whether it is pure or not doesn't really matter). Just ensure never to return a reference to a temporary.

If some derived class needs to perform a costly computation, it can still store the result in a member std::wstring and return a reference to that. But once again, don't guess performance issues: measure first and focus on it only if this seems to be the bottleneck.

歌枕肩 2024-11-24 03:11:28

尽管其他人似乎都同意这是一个非常糟糕的主意(出于多种原因,所有这些都有些正确),但我不认为返回字符串有任何根本错误常量&。

这样做

  • 在技术上是合法的,并且工作得很好(它只是制定了一个“合同”,派生类必须以某种尚未指定的方式履行)
  • 可能是一个微不足道的微观优化(这也可能与 RVO 没有什么不同)优化构建),但它也没有伤害......到目前为止,所以什么
  • 不允许调用者修改名称,这是有道理的(调用者没有能力这样做,这就是为什么他必须在第一个询问地方!)——这在语义上实际上是一件好事,
  • 无论实际的未知实现如何,即使是临时变量(因为对于 const 引用,标准将局部变量的生命周期延长到生命周期 因此,虽然有点不寻常

,但如果它做了你想要的事情(特别是明确调用者不能修改字符串),我认为没有绝对的理由不这样做。

Though everyone else seems to agree that it is a very bad idea (for a variety of reasons, all of which are somewhat true), I don't see anything fundamentally wrong with returning a string const&.

Doing so

  • is technically legitimate and will work just fine (it merely lays down a "contract" that the deriving classes must fulfill in some yet-unspecified way)
  • is probably an insignificant micro-optimization (which also is likely none different from RVO in an optimized build), but it does not hurt either... insofar, so what
  • does not allow the caller to modify the name, which makes sense (the caller is not competent to do so, it's why he had to ask in the first place!) -- this is semantically actually a good thing
  • will work regardless of the actual yet-unknown implementation, even with temporaries (since for const references, the standard extends the lifetime of local variables to the lifetime of the reference)

So, although kind of unusual, if it does what you want (especially making explicit that the caller may not modify the string), I see no absolute reason not to do it.

携余温的黄昏 2024-11-24 03:11:28

好在什么意义上?这是合法的C++。这通常是非常糟糕的编程
实践,出于你提到的原因:它强加了(通常)不必要的
对派生类的限制。独立于虚拟,您应该
仅当函数的语义需要时才返回引用;
如果类型具有值语义,例如 std::string,那么这将
只能作为非常量引用。 (模板使问题有些复杂化,
std::vector 这样的类返回一个是合理的
operator[] const 引用 const。)

OK in what sense? It's legal C++. It's usually very poor programming
practice, for the reason you mention: it imposes a (usually) unnecessary
restriction on derived classes. Independently of virtual, you should
only return a reference when the semantics of the function require it;
if the type has value semantics, like std::string, then this would
only be a non-const reference. (Templates complicate the issue somewhat,
and it is reasonable for classes like std::vector to return a
reference to const from operator[] const.)

格子衫的從容 2024-11-24 03:11:28

这似乎是个坏主意。如果开销确实很重要,您可以使用一些具有隐式共享(写时复制语义)的字符串容器,例如 QString。

It seems like a bad idea. If the overhead really matters, you could use some string container with implicit sharing (copy-on-write semantics), like QString.

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