作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
假设我有一个员工树,每个员工都有同事,同事有同事.....我设置了两个类employee用来存储员工的信息和eTree 表示树架构。
每个员工都有一个 vector 存储智能指针指向他们的同事。看下面的代码。为什么智能指针指向的地址最后变成了00000000。我已将指针返回到函数 buildChilds 之外。
#include <iostream>
#include <string>
#include <vector>
#include <memory>
class employee
{
std::string NAME;
int NUM;
int Enum = 0;
public:
std::vector<std::shared_ptr<employee>> childs;
employee(std::string name, int num) :NAME(name), NUM(num)
{
childs.resize(num);
}
std::string getName()
{
return NAME;
}
int getNum()
{
return NUM;
}
void setEnum(int num)
{
Enum = num;
}
int getEnum()
{
return Enum;
}
};
class eTree
{
private:
std::shared_ptr<employee> proot;
public:
eTree(employee node) { proot = std::make_shared<employee> (node); }
void buildTree(std::shared_ptr<employee> parent);
std::shared_ptr<employee> getP() { return proot; }
std::shared_ptr<employee> buildChilds(employee &parent, int i);
};
void eTree::buildTree(std::shared_ptr<employee> parent)
{
int num = parent->getNum();
if (num != 0)
{
for (int i = 0; i < num; i++)
{
(*parent).childs[i] = buildChilds(*parent, i);
std::cout << (*parent).childs[i] << std::endl;
buildTree((*parent).childs[i]);
}
int eNum = (*parent).getNum();
for (int j = 0; j < num; j++)
{
eNum += (*((*parent).childs[j])).getEnum();
}
(*parent).setEnum(eNum);
}
}
std::shared_ptr<employee> eTree::buildChilds(employee &parent, int i)
{
std::string name;
int num;
std::cout << "Enter the name of " << parent.getName() << "'s " << i + 1 << " child:" << std::endl;
std::cin >> name;
std::cout << "How many employee work for " << parent.getName() << "'s " << i + 1 << " child:" << std::endl;
std::cin >> num;
// employee child(name, num);
std::shared_ptr<employee> pchild (new employee(name, num));
// std::shared_ptr<employee> pchild = std::make_shared<employee>(child);
return pchild;
}
void more3(employee parent);
void noChild(employee parent);
int main()
{
using namespace std;
std::string name;
cout << "Enter the employee's name: ";
cin >> name;
int num;
cout << "How many employees work for him/her: ";
cin >> num;
employee root(name, num);
shared_ptr<employee> proot = make_shared<employee> (root);
eTree tree(root);
tree.buildTree(proot);
cout << tree.getP()->childs[0] << endl;
cout << tree.getP()->childs[1] << endl;
return 0;
}
可以输入a->2->b->0->c->0,然后查看结果;谢谢!
最佳答案
问题是您正在创建根员工的许多拷贝并在不同的地方使用这些拷贝。
在您的主函数中,尝试以下操作:
shared_ptr<employee> proot = make_shared<employee> (name, num);
eTree tree(proot);
tree.buildTree(proot);
然后更新你的eTree
构造函数:
eTree(std::shared_ptr<employee> node) { proot = node; }
我的推理:
在您的代码中,您似乎创建了一名新员工:
employee root(name, num);
然后你制作一个拷贝但是一个智能指针:
shared_ptr<employee> proot = make_shared<employee> (root);
然后将原件提供给 eTree:
eTree tree(root);
并复制到 buildTree 函数:
tree.buildTree(proot);
因此,构建树为另一个员工拷贝构建树
编辑:您的 eTree 构造函数还制作了另一个拷贝作为智能指针,因此最终您得到了原始员工的 3 个实例。
如下删除employee的拷贝构造函数,你会看到会发生什么:
employee(const employee&) = delete;
我的代码会编译,你的代码不会...
编辑:显示 std::make_shared<>()
的示例代码构造一个新对象:
#include <memory>
#include <iostream>
class MyClass {
public:
MyClass() { std::cout << " -> MyClass Constructor called\n"; }
MyClass(const MyClass& other) { std::cout << " -> MyClass Copy Constructor called\n"; }
};
int main(int argc, char *argv[])
{
std::cout << "[1] Making MyClass\n";
MyClass myClass;
std::cout << "[2] Making MyClass Pointer\n";
MyClass* myClassPtr = new MyClass();
std::cout << "[3] Making MyClass Shared using New\n";
std::shared_ptr<MyClass> myClassSmart1 = std::shared_ptr<MyClass>(new MyClass());
std::cout << "[4] Making MyClass Shared\n";
std::shared_ptr<MyClass> myClassSmart2 = std::make_shared<MyClass>();
std::cout << "[5] Making MyClass Shared from first MyClass\n";
std::shared_ptr<MyClass> myClassSmart3 = std::make_shared<MyClass>(myClass);
std::cout << "Done\n";
}
以上代码证明std::make_shared<T>()
构造一个新对象,而不是仅仅为一个已经存在的对象创建一个共享指针。另外,std::make_shared<Class>(class)
使用复制构造函数创建新对象。
以上在我的电脑上的输出是:
[1] Making MyClass
-> MyClass Constructor called
[2] Making MyClass Pointer
-> MyClass Constructor called
[3] Making MyClass Shared using New
-> MyClass Constructor called
[4] Making MyClass Shared
-> MyClass Constructor called
[5] Making MyClass Shared from first MyClass
-> MyClass Copy Constructor called
Done
关于c++ - 如何使用智能指针构建树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33749395/
我是一名优秀的程序员,十分优秀!