传递 std::vector of std::shared_ptr,而不是更新对象
Passing std::vector of std::shared_ptr, not updating the objects
好吧,我可能做错了,但我已经到了我的智慧尽头。
我有一个节点类的 shared_ptr 向量,我为各种事情传递它,我的节点类有一个 share_ptr 节点类型的邻居的向量。
我有一个类,它为我生成节点网格,并返回一个std::vector<std::shared_ptr<Node>> nodes
和一个重要的std::shared_ptr<Node> significant node
。 然后,我将这个向量传递到索引器中,该索引器创建第二个列表,该列表是第一个列表的子集,大小约为大小的 10%,它以std::vector<std::shared_ptr<Node>> indexedNodes
的形式返回。
创建它们后,我将它们传递到另一个对象中,该对象保留它们以供以后参考。 然后,修饰符类从 indexedNode 获取单个随机节点,并使用它来遍历修改高度值的节点邻居。
稍后,当我将它们导出时,值显示为 0/已初始化。
需要注意的是,我将数据传递到函数中并返回std::vector<std::shared_ptr<Node>>
我认为这是我的问题,我只是不确定如何正确传递我的shared_ptr容器,以便我不做副本。
如果需要更多信息,请告诉我。我正在寻找一个我可以理解的例子或参考。
对不起,代码,它并不漂亮,我使用动态加载的库拥有它。
完成工作的函数:
void cruthu::Cruthu::Run() {
std::shared_ptr<cruthu::ITeraGen> teraGen(this->mSettings.TeraGen.Factory->DLGetInstance());
std::vector<std::shared_ptr<cruthu::Node>> nodes(teraGen->Create());
std::shared_ptr<cruthu::Node> significantNode(teraGen->GetSignificantNode());
std::vector<std::shared_ptr<cruthu::IIndexer>> indexers;
for(const auto indexer : this->mSettings.Indexers) {
indexers.push_back(indexer.Factory->DLGetInstance());
}
std::vector<std::shared_ptr<cruthu::Node>> indexedNodes(indexers.at(0)->Index(nodes));
std::shared_ptr<cruthu::ITera> tera(this->mSettings.Tera.Factory->DLGetInstance());
tera->SetNodes(nodes);
tera->SetIndexedNodes(indexedNodes);
tera->SetSignificantNode(significantNode);
for(const auto & formaF : this->mSettings.Formas) {
std::shared_ptr<cruthu::IForma> forma(formaF.Factory->DLGetInstance());
forma->SetNode(tera->GetIndexedNode());
forma->Modify();
std::cout << std::to_string(tera->GetIndexedNode()->GetHeight()) << std::endl;
}
this->CreateImage(tera);
}
太世代:
#ifndef CRUTHU_ITERAGEN_HPP
#define CRUTHU_ITERAGEN_HPP
#include <cruthu/Node.hpp>
#include <vector>
namespace cruthu {
class ITeraGen {
public:
virtual ~ITeraGen() = default;
virtual std::vector<std::shared_ptr<cruthu::Node>> Create() = 0;
virtual std::shared_ptr<cruthu::Node> GetSignificantNode() = 0;
};
} // namespace cruthu
#endif
泰拉:
#ifndef CRUTHU_ITERA_HPP
#define CRUTHU_ITERA_HPP
#include <cruthu/IIndexer.hpp>
#include <cruthu/Node.hpp>
#include <memory>
#include <vector>
namespace cruthu {
class ITera {
public:
virtual ~ITera() = default;
virtual void SetNodes(std::vector<std::shared_ptr<cruthu::Node>>& nodes) = 0;
virtual void SetIndexedNodes(std::vector<std::shared_ptr<cruthu::Node>>& indexedNodes) = 0;
virtual void SetSignificantNode(std::shared_ptr<cruthu::Node> significantNode) = 0;
virtual std::vector<std::shared_ptr<cruthu::Node>>& GetNodes() = 0;
virtual std::vector<std::shared_ptr<cruthu::Node>>& GetIndexedNodes() = 0;
virtual std::shared_ptr<cruthu::Node> GetIndexedNode() = 0;
};
} // namespace cruthu
#endif
索引:
#ifndef CRUTHU_IINDEXER_HPP
#define CRUTHU_IINDEXER_HPP
#include <cruthu/Node.hpp>
#include <memory>
#include <vector>
namespace cruthu {
class IIndexer {
public:
virtual ~IIndexer() = default;
virtual std::vector<std::shared_ptr<cruthu::Node>> Index(std::shared_ptr<cruthu::Node> node) = 0;
virtual std::vector<std::shared_ptr<cruthu::Node>> Index(std::vector<std::shared_ptr<cruthu::Node>>& nodes) = 0;
};
} // namespace cruthu
#endif
福尔玛:
#ifndef CRUTHU_IFORMA_HPP
#define CRUTHU_IFORMA_HPP
#include <cruthu/Node.hpp>
namespace cruthu {
class IForma {
public:
virtual ~IForma() = default;
virtual void SetNode(std::shared_ptr<cruthu::Node> node) = 0;
virtual void Modify() = 0;
};
} // namespace cruthu
#endif
我确实更新并尝试在两者之间添加引用,这就是为什么它们现在在某些地方都有引用的原因。我仍然有同样的问题。
正如用户Remy Lebeau
所述,请提供一个最小、完整和可验证的示例。
正如您所说,您正在将std::vector<std::shared_ptr<Node>>
从一个类传递到另一个类或从一个函数传递到另一个函数,并且它们没有更新并且初始化为 0。从您描述的行为来看,我有一个问题要问您,我将其作为答案发布,因为评论太长了。
接受上述向量或共享指针的函数声明/定义是否如下所示:
void someFunc( std::vector<shared_ptr<Node> nodes ) { ... }
或者它看起来像这样:
void someFunc( std::vector<shared_ptr<Node>& nodes ) { ... }
我问这个是因为如果您按值而不是按引用传递容器,这会有所不同。
这不是(还(一个答案,而是确定问题的问题,因为没有提供足够的实现。
一个可能的问题(如果不看到实现就很难说...(是你在Run()
函数之上创建节点:
std::vector<std::shared_ptr<cruthu::Node>> nodes(teraGen->Create());
然后,在此调用中将该函数作为引用传递:
tera->SetNodes(nodes);
tera 如何处理节点?按引用传递意味着 shared_ptr:s 的计数不会递增。
this->CreateImage(tera)
做什么?
节点是否在Run()
完成后使用?
我无法通过上面的评论进行它,这主要是我无法提供足够信息的问题。 话虽如此,我重新编写了代码,改为将cruthu::Tera
对象作为共享指针传递,并将向量公开为类的公共成员。这是我稍后会重新讨论的内容,因为这种实现并不是我感到高兴的事情。
代码在github上,不幸的是这是我的论文工作,所以我不能再等了。
如果人们仍然想尝试答案,我会与他们合作。
- 将对象移动到std::shared_ptr
- 使用std::函数映射对象方法
- 使用"std::unordereded_map"映射到"std::list"对象
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 当使用透明的std函数对象时,我们还需要写空的尖括号吗
- 如何使用std::min和std::less返回对象
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- C++11 中不同类型的对象的 std::array 的替代方案
- 返回一个带有 std::move 的对象并链接函数
- std::vector::p ush_back() 不会在 MSVC 上编译具有已删除移动构造函数的对象
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- std::string 的对象真的可以移动吗?
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 即发即弃 std::线程对象清理自身
- 当 std::move 与 C 样式数组或不移动对象时会发生什么
- 传递 std::vector of std::shared_ptr,而不是更新对象
- std::对象映射或对象指针
- 来自std ::对象向量的基础数据
- 在std::对象列表中快速搜索和删除
- 在不同 DLL 之间传递的 std 对象