我有这门课(我知道,太可怕了..我刚开始使用 c++ 中的类,指针对我来说有点难了.. :) ):
class Tree{
private:
Tree* Left;
Tree* Right;
int info;
Tree* GetTree(int num){
return new Tree(num);
}
public:
Tree() :info(0) ,Left(nullptr), Right(nullptr) {};
Tree(int num) : info(num), Left(nullptr) , Right(nullptr){};
Tree(int num,Tree* L,Tree* R) : info(num), Left(L), Right(R) {};
void SetLeft(Tree* L){Left = L;};
void SetRight(Tree* R){Right = R;};
void SetLeft(Tree L){Left = &L;};
void SetRight(Tree R){Right = &R;};
void SetLeft(int num){Left = GetTree(num);};
void SetRight(int num){Right = GetTree(num);};
void SetInfo(int num){info = num;};
Tree GetLeft(){return (Left==nullptr) ? Tree() : *Left;};
Tree GetRight(){return (Right==nullptr) ? Tree() : *Right;};
Tree* GetLeft(bool ptr){return Left;};
Tree* GetRight(bool ptr){return Right;};
int GetInfo(){return info;};
};
(基本上这个类创建一棵树,它有 2 片叶子(另外 2 棵树),左和右,它们每个都有更多 2 片叶子,依此类推..)所以我这样做了:
Tree temp(5);
Tree* tem = &temp;
temp.SetInfo(10);
temp.SetLeft(5);
temp = temp.GetLeft();
cout << tem->GetInfo();
希望tem指向树的头部,但它只是跟随温度.. 有没有办法轻松做到这一点?
你们能帮我上课吗?我很难将指针和类 S 结合起来:
谢谢!