-1

I have a code class below which represents a node in a binary tree:

//btnode class

class btnode {
public:
    btnode(int data): m_info(data), m_left(nullptr), m_right (nullptr) {}

    btnode* get_left_btnode ()  { return this->m_left; }
    btnode* get_right_btnode () { return this->m_right; }
    int get_data () { return this->m_info;}
private:
    int m_info;
    btnode* m_left;
    btnode* m_right;
};

I've a questions! how would it matter if i had members functions like follows:

    btnode* get_left_btnode ()  { return m_left; }
    btnode* get_right_btnode () { return m_right; }
    int get_data () { return m_info;}

any other suggestion of how efficiently write this treenode will be helpful

Amaresh Kumar
  • 586
  • 1
  • 8
  • 20
  • Code review question are more suitable on [Code Review](https://codereview.stackexchange.com/help/dont-ask). – xskxzr Apr 27 '18 at 10:14
  • 2
    Those getter functions are useless. Make the members public! – DeiDei Apr 27 '18 at 10:26
  • @DeiDei thanks. only i've already realised that. after when i faced issue of pointing an updated left child node to newly added node. guess (lvalue problem). – Amaresh Kumar Apr 27 '18 at 12:57

1 Answers1

1

If you are asking if the explicit dereferencing of this is needed (this->), then the answer is No. It's completely optional and makes no difference in this case.

There are situations where this-> makes a difference though. For example if you have a local variable named the same as a member variable, then you'd need this-> to refer to the member (but a better solution would be to name your variables uniquely).

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • can you please write a small example where it will impact the correctness of the result and will differ. Scope: the question. thanks for the answer though – Amaresh Kumar Apr 27 '18 at 11:08
  • It makes a difference e.g. here: `void set_info(int m_info) { this->m_info = m_info };` (without `this` the member `m_info` is hidden) - Now this is obviously not needed since you wouldn't name the parameter `m_info` but you get the point. – Max Langhof Apr 27 '18 at 11:18
  • @Max Langhof I'm quite well aware of that. I said only that it does not matter *here* with the 3 get functions OP asked about. Of course there are situations where `this->` makes a difference. – Jesper Juhl Apr 27 '18 at 11:24
  • I meant to answer @Amaresh's comment of "small example where it matters". Sorry for the confusion! – Max Langhof Apr 27 '18 at 11:26