already_AddRefed 编辑
already_AddRefed
in association with nsCOMPtr
allows you to assign in a pointer without AddRef
ing it.
You may want to use this as a return type from a function that has already produced an AddRef
ed pointer as a result. In fact, it is preferred to use already_AddRefed
in this case over returning a raw pointer or nsCOMPtr
(see the nsCOMPtr user manual).
Defined in: mfbt/AlreadyAddRefed.h
.
Example
...
already_AddRefed<nsIFoo> GetFoo() {
nsIFoo* foo = mFoo;
NS_IF_ADDREF(foo);
return foo;
}
// or
already_AddRefed<nsIFoo> GetFoo() {
nsCOMPtr<nsIFoo> foo = mFoo;
// ...
return foo.forget();
}
...
// The following assignment doesn't perform an additional AddRef,
// as it would do if GetFoo() returned a raw pointer.
nsCOMPtr<nsIFoo> foo = GetFoo();
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论