简体   繁体   English

我的问题是关于类成员函数的返回类型作为指向类的指针

[英]My question is about the return type of a class member function as a pointer to class

I have a question about return type of a function as a pointer to class that how it works.我有一个关于函数的返回类型作为指向它如何工作的类的指针的问题。 Especially treenode* get_left()尤其是treenode* get_left()

class treenode{
private:
   int object;
   treenode* left;
   treenode* right;

public:
   treenode* get_left(){
      return this->left;
   }
};

This function returns a pointer of type Treenode which have 3 attributes: int object, treenode* left and treenode* right.此函数返回一个 Treenode 类型的指针,它具有 3 个属性:int object、treenode* left 和 treenode* right。

You can use this pointer as object and call their function.您可以将此指针用作对象并调用它们的函数。

That is to say, if you have the following tree:也就是说,如果你有下面这棵树:

        6 
     4     9

if you do : SixTree.get_left() it returns a pointer to treenode (FourTree in this case) and now you can use: print(FourTree.object) and the result should be :如果你这样做: SixTree.get_left() 它返回一个指向 treenode (在这种情况下为 FourTree )的指针,现在你可以使用: print(FourTree.object) 并且结果应该是:

>> 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM