- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C++ 的新手,正在尝试了解引用计数内存。在下面的代码中,我有两种返回矩形 vector 的方法
vector<RectangleRef> &rectanglesRef() { return rects_; }
vector<RectangleRef> rectangles() { return rects_; }
我不确定的是他们中的任何一个是否在堆栈上为调用者创建了一个新的 vector ?
当您的 ClassA 在您希望能够公开的 vector 中包含许多 ClassB 时,推荐的模式是什么?你有吗
void addRectangle(RectangleRef r) { rects_.push_back(r); }
void removeRectangle(RectangleRef r);
vector<RectangleRef> rectangles() { return rects_; }
或者您只是让调用者访问内部结构并允许他们随意添加/删除内容?
如果 Rectangle 类要保留指向它所属的 Test 类的后向指针,则正在使用
typedef std::weak_ptr<Test> TestWeakRef;
...
TestWeakRef test_;
正确的成语?
感谢您的帮助。
代码:
#include <iostream>
#include <limits>
#include <vector>
using namespace std;
class Point {
public:
Point() {
x_ = 0.0;
y_ = 0.0;
}
Point(double x, double y) {
x_ = x;
y_ = y;
}
double x() const { return x_; }
double y() const { return y_; }
void setX(double x) { x_ = x; }
void setY(double y) { y_ = y; }
void offset(double dx, double dy) {
setX(x() + dx);
setY(y() + dy);
}
bool operator == (Point const &point) {
return x() == point.x() && y() == point.y();
}
void operator = (Point const &point) {
setX(point.x());
setY(point.y());
}
private:
double x_;
double y_;
};
class Size {
public:
Size() {
width_ = 0.0;
height_ = 0.0;
}
Size(double width, double height) {
width_ = width;
height_ = height;
}
double width() const { return width_; }
double height() const { return height_; }
double area() const { return width() * height(); }
void setWidth(double width) { width_ = width; }
void setHeight(double height) { height_ = height; }
private:
double width_, height_;
};
class Rectangle {
public:
Rectangle() {
origin_ = Point();
size_ = Size();
}
Rectangle(double x, double y, double width, double height) {
origin_ = Point(x, y);
size_ = Size(width, height);
}
Point origin() const { return origin_; }
Size size() const { return size_; }
private:
Point origin_;
Size size_;
};
typedef std::shared_ptr<Rectangle> RectangleRef;
typedef std::weak_ptr<Rectangle> RectangleWeakRef;
class Test {
private:
vector<RectangleRef> rects_;
public:
Test() {
rects_ = vector<RectangleRef>();
for (int i = 0; i < 100; i++) {
RectangleRef ptr = make_shared<Rectangle>(i*1.0, 0.0, 1.0, 1.0);
rects_.push_back(ptr);
}
}
vector<RectangleRef> &rectanglesRef() { return rects_; }
vector<RectangleRef> rectangles() { return rects_; }
};
int main(int argc, const char * argv[]) {
vector<RectangleRef> r;
vector<RectangleRef> r1;
if (true) {
Test t = Test();
r = t.rectangles();
r1 = t.rectanglesRef();
if (r1 == r) { cout << "they match\n"; }
}
// insert code here...
//std::cout << r->origin().x() << "\n";
return 0;
}
最佳答案
Drew Dormann 很好地回答了前几个问题。我只是添加一些内容。
首先,你真的应该只使用 shared_ptr
如果您确实需要共享所有权语义。引用计数有开销,如果你没有正确地分解循环依赖,它会导致内存泄漏,所以你不应该这样做,除非你确实需要它。
在这段代码中,没有真正的理由需要存储 vector<RectangleRef>
而不是简单的 vector<Rectangle>
.后者还具有更好的数据局部性优势,这对于缓存很重要。
二、这个
typedef std::weak_ptr<Test> TestWeakRef;
// ...
TestWeakRef test_;
通常不是一个好主意,因为它限制了 Test
的生命周期管理策略。 .也就是说,因为 weak_ptr
只能引用一个生命周期由shared_ptr
管理的对象,你只是让它不可能做像 Test t;
这样的事情或 unique_ptr<Test> t;
.除非你有非常非常好的理由,否则不要这样做。
如果你想要反向指针,只需使用一个简单的非拥有原始指针,无论 Test
是否存在,它都可以工作。对象在堆栈上或由 unique_ptr
管理或 shared_ptr
或其他一些自定义智能指针:
Test * test_;
如果 Rectangle
的生命周期s 存储在 Test
中可以超过 Test
的生命周期(这只有在您不拥有它们的独占所有权时才会发生(例如,将 shared_ptr
存储给它们),这通常是不必要的),您的 Test
析构函数可能必须经过 Rectangle
它存储并清除指针以防止悬空指针。但典型情况是用 Test
存储的内容当对象被销毁时对象将消失,在这种情况下这是不必要的。
关于c++ - 父子关系所有权的推荐模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24215850/
我正在使用 tcod-rs。用于绘制到 RootConsole 的每个方法都采用一个可变引用。中央循环是一个 while 循环,它等待窗口关闭、清除屏幕、绘制,然后刷新。 “检查窗口关闭”方法也采用可
我写了一个具有这种形式的函数: 结果 f(const IParameter& p); 我的意图是这个签名将明确表明函数没有取得参数 p 的所有权。 问题是 Result 将保留对 IParameter
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: What is a smart pointer and when should I use one? 我正在阅
假设我有一个类: class Scheduler { Scheduler(JobService *service); AddJob(JobID id, ISchedule *sched
我试图弄清楚所有权如何与函数 CVMetalTextureGetTexture 一起工作: CVMetalTextureRef textureRef; // ... textureRef is cre
这个问题在这里已经有了答案: Should we pass a shared_ptr by reference or by value? (10 个答案) 关闭 4 年前。 例如 class A {
我正在做一个附带项目,我需要根据他的 gmail 帐户或任何其他参数来验证 channel 是否属于某个用户……这基本上是为了避免假帐户。是否可以? 最佳答案 是的, 跟随 youtube 记录的链接
我在使用Core Foundation Array时发现了一个奇怪的问题!这是代码片段 fname = CFStringCreateWithFormat(kCFAllocatorDefault, NU
有没有一种方法可以设置在 apache 下运行的 php 来创建文件夹,该文件夹的文件夹属于创建它的程序的所有者,而不是由 apache 拥有? 使用 word press 它会创建要上传到的新文件夹
我编写了以下函数来使用 boost.date_time 获取日期/时间字符串. namespace bpt = boost::posix_time; string get_date_time_stri
我在使用 Docker 容器时遇到了一个有点烦人的问题(我在 Ubuntu 上,所以没有像 VMWare 或 b2d 这样的虚拟化)。我已经构建了我的镜像,并且有一个正在运行的容器,它有一个来 sel
根据大多数示例,逻辑上最少有 3 个组织 ( org1, org2, orderer )。 实际上只有 2 个物理组织 ( org1, org2 )。任一组织或约定的第 3 方必须移交订购者组织的职责
我开始学习 Rust,在进行实验时,我发现所有权如何应用于我不理解的元组和数组的方式有所不同。基本上,以下代码显示了差异: #![allow(unused_variables)] struct Inn
我们有一个应用程序,其表单上有许多组件(面板、选项卡、编辑、组合框等)。但根据用户配置文件,其中大多数可以自动填充和/或不可见。因此,用户可以更快地完成工作。 问题:是否有更简单的方法可以在运行时创建
我有以下代码片段: fn f u32>(c: T) { println!("Hello {}", c()); } fn main() { let mut x = 32; let
我想执行示例中的代码: require_once 'google-api-php-client/vendor/autoload.php'; $client = new Google_C
这个问题在这里已经有了答案: What is move semantics? (11 个答案) 关闭 3 年前。 我有一个看起来像这样的构造函数: Thing::Thing(std::vector
我们正在使用服务帐户从服务器上传文件,但它已达到其存储配额限制。所有文件都已添加到另一个用户(具有 100 Gb 存储配额的 @gmail.com 帐户)创建的文件夹下,但上传的所有文件均归该服务帐户
我正处于 this question 中描述的 sme 情况。 .那个提问者找到的解决方案是 Full access !== Owner. I need to read the documentati
我正处于 this question 中描述的 sme 情况。 .那个提问者找到的解决方案是 Full access !== Owner. I need to read the documentati
我是一名优秀的程序员,十分优秀!