C++返回对非复制对象向量的引用
我有class a ,带有禁用的复制语义和类b
,其中包含a
's的向量。如何编写B的成员函数,该功能返回引用a
的向量。
对于那些知道这里生锈的人是我要做的事情(用锈语表达):
struct A {/* ... */}
struct B {
data: Vec<A>,
}
impl B {
pub fn data(&self) -> &Vec<A> {
&self.data
}
}
I have class A
with disabled copy semantics and class B
which contains vector of A
's. How can I write a member function of B that returns reference to the vector of A
's?.
For those knowing Rust here is what I am trying to do (expressed in the Rust language):
struct A {/* ... */}
struct B {
data: Vec<A>,
}
impl B {
pub fn data(&self) -> &Vec<A> {
&self.data
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

正如@molbdnilo所说的那样,我的错误是我使用了
auto data = b.data();
而不是auto&amp; data = b.data();
。因此,以下示例正在工作,并显示如何做我问的事情:As @molbdnilo stated my error was that I used
auto data = b.data();
instead ofauto& data = b.data();
. Therefore following example is working and shows how to do what I asked: