可以从纯虚函数返回引用吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这不是真的。编译器通常能够使用返回值优化来消除副本。
This is not true. The compiler will very often be able to elide the copy using Return Value Optimization.
不要猜测性能问题:虽然人们经常通过 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.尽管其他人似乎都同意这是一个非常糟糕的主意(出于多种原因,所有这些都有些正确),但我不认为返回字符串有任何根本错误常量&。
这样做
,但如果它做了你想要的事情(特别是明确调用者不能修改字符串),我认为没有绝对的理由不这样做。
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
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.
好在什么意义上?这是合法的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 shouldonly return a reference when the semantics of the function require it;
if the type has value semantics, like
std::string
, then this wouldonly be a non-const reference. (Templates complicate the issue somewhat,
and it is reasonable for classes like
std::vector
to return areference to const from
operator[] const
.)这似乎是个坏主意。如果开销确实很重要,您可以使用一些具有隐式共享(写时复制语义)的字符串容器,例如 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
.