Reputation: 1062
Assume that I have a employee tree and every employees have coworkers, coworkers have coworkers ..... I set up two class employee using to store employee's information and eTree represent the tree architecture.
Every employee has a vector stores smart pointer pointing to their coworkers. Look at following code. Why the address the smart pointer pointing become 00000000 finally. I've return the pointer out of function 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;
}
You could input a->2->b->0->c->0, and then see the result; thanks!
Upvotes: 2
Views: 385
Reputation: 3950
The issue is that you are creating many copies of the root employee and using the copies in different places.
In your main function, try the following:
shared_ptr<employee> proot = make_shared<employee> (name, num);
eTree tree(proot);
tree.buildTree(proot);
And then update your eTree
constructor:
eTree(std::shared_ptr<employee> node) { proot = node; }
My reasoning:
In your code, it appears that you create a new employee:
employee root(name, num);
Then you make a copy of that but a smart pointer:
shared_ptr<employee> proot = make_shared<employee> (root);
Then you give the original to the eTree:
eTree tree(root);
and the copy to the buildTree function:
tree.buildTree(proot);
Thus, build tree builds the tree for another copy of employee
edit: Also your constructor for eTree makes yet another copy as a smart pointer, so in the end you end up with 3 instances of the original employee.
Remove the copy constructor of employee as follow and you will see what happens:
employee(const employee&) = delete;
My code will compile, your code will not...
Edit: Example code to show that std::make_shared<>()
construct a new object:
#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";
}
The above code proves that std::make_shared<T>()
constructs a new object instead of just creating a shared pointer for an already existing object.
Also, std::make_shared<Class>(class)
uses the copy constructor to create the new object.
The output of the above on my computer is:
[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
Upvotes: 1