类 Vector2 最终:shared_ptr<Vector2>正确与否(更新!
class Vector2 final : shared_ptr<Vector2> correct or not (Updated!)
这样做的原因是,我想在使用此类时强迫所有对象成为智能指针。因此,我可以保存一些代码,例如
class Vector2 final : shared_ptr<Vector2>
{
...
inline shared_ptr<Vector2> getVector2(); //--> inline Vector2 getVector2();
...
inline static float dot(const shared_ptr<Vector2> a, const shared_ptr<Vector2> b); //--> inline static float dot(const Vector2 a, Vector2 b);
}
正确吗?如果是,这是最好的做法吗?如果没有,最好的是什么?
==========================================================================================
也许我使用另一个示例
class Sprite
{
private:
Vector2* pPosition;
shared_ptr<Vector2> position;
Image* pBackground;
shared_ptr<Image> background;
};
让我解释为什么我需要参考计数。当我更新位置或背景时,我需要在分配新值之前处理删除操作。
这个
怎么样inline static shared_ptr<Vector2> add(const Vector2* a, const Vector2* b)
这将返回在退出功能之前可能会或可能不会使用的结果。然后,如果不使用它,我需要手动删除。
代码保存是指1.无需手动删除对象2.不需要关心它是共享_ptr还是vector2*。因为我想标准化为一种类型
我不明白如何为您节省任何东西。为什么您的矢量根本需要被参考?对于您从根本上不需要的东西来说,这确实很昂贵,这使您的代码语义变得复杂,并且只会使读者变得WTF。
问题不是vector2
是否应该是共享指针,或者是否应该将其包裹在共享指针中。没有理由做。
我认为您的设计非常糟糕。好像您想拥有Java寿命或其他东西。
您需要考虑到关注点的分开设计。
您的功能dot
不应关心如何管理向量的寿命2,只需通过参考将vector2对象插入,然后将它们包裹在 std::shared_ptr
s中,然后在使用之前将其fefreference defreference。
在同一徒劳中,您的vector2对象不应对如何管理其生命周期。
相关文章:
- 从C++本机插件更新Vector3数组
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 如何使用AngelScript注册SFML Vector2运算符
- cmake更新缓存的变量
- 更新到莫哈韦后出现cmath错误
- OpenMP:并行更新数组总是需要减少数组吗
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 指针没有更新它在void函数内部指向的值
- 如何在c++中获取要更新的值
- 已修改的LinkedList未在文本文件本身中更新
- Qt:当QListView获得新条目时,如何更新QStringList
- 更新的矢量元素不打印
- 如何使用按钮更新GTK3图像以使用C++从相机捕获图片
- 如何在ECS框架中更新组件数据和通知系统
- 从VS 2015更新3更新到VS2015更新3 d后浮点计算行为不同的原因
- 如果我想在没有更新编译器的情况下使用新功能,该怎么办?
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 在 emscripten 网页汇编正在运行期间更新进度条?
- 类 Vector2 最终:shared_ptr<Vector2>正确与否(更新!