何时应该使用shared_ptr和unique_ptr?
例如,在这个类中,node* 应该改用shared_ptr或unique_ptr。这取决于什么?
class node
{
private:
node *parent;
vector<node*> children;
/*
* txny
* x - numer drzewa
* y - numer wezla
*/
string id;
typeNode type; //0 - term, 1 - func
public:
node(node* parent, string id, typeNode type);
virtual ~node() {}
void addChild(node* child);
void setChildren(vector<node*> &children);
node* getChild(int i);
int getChildrenNumber();
void setParent(node* parent);
node* getParent();
string getId();
typeNode getType();
void setId(string id);
};
编辑:
类树拥有节点对象。我必须写更多的文字,因为无法保存更改。
class tree
{
private:
vector <node*> nodes;
int depth;
int counterNodes;
/*
* pxty
* x - numer populacji (generacji)
* y - numer drzewa
*/
string id;
private:
node* root;
node* parentGeneticPoint;
public:
tree(int depth, string id);
tree(int depth);
~tree();
public:
void initialize(typeInit type);
void showVector();
void show();
Mat run();
tree* copy();
tree* copySubtree(int subrootI);
tree* copyWithoutSubtree(int subrootI);
};
node
对象?所有权语义通常指导使用哪种指针类型。 - Mark B