Can I use a member element as the default argument for a class method?

The Minimum method returns the minimum element in the binary search tree. If the argument is not passed, it prints the minimum of the caller. If the node address is passed, it prints a minimum of the subtree whose root is node

When compiling, it shows "Invalid use of non-static Tree::root data element"

 #include<stdlib.h> #include<iostream> class Node { public: Node *leftchild; Node *rightchild; Node *parent; int info; }; class Tree { public: Node *root; Tree() { root=NULL; } void Minimum(Node*); }; void Tree::Minimum(Node *curnode=root) { Node *parent; while(curnode!=NULL) { parent=curnode; curnode=curnode->leftchild; } std::cout<<parent->info<<endl; } int main() { Tree tree; tree.Minimum(); return 0; } 
+4
source share
4 answers

No, you can’t.

For the default value, you can use either a value, a variable, or a function that is available in the context of a function definition that is in the class definition that is outside of any specific context of the object.

This usually helps me think about how the compiler really handles this. In particular, when the compiler performs an overload for a function and detects an overload with more arguments than those used at the call site, the compiler will generate code at the call site to fill in the remaining arguments. The generated code will always generate a call with all arguments:

 int g(); void f(int x = g()); int main() { f(); // [1] } 

When the compiler processes [1] and has overload permission, it discovers that void ::f(int x = g()) is the best candidate and selects it. Then it fills in the default argument and generates a call for you:

 int main() { f( /*compiler injected*/g() ); } 

If you consider calling a member function or a member variable of a class, this does not make sense in the context of the caller (the language can be changed to adapt to this, it is impossible to handle this, but it does not work with the current model).

+3
source

You can also set it to NULL , for example, by default, and then check and set it to a member in the method.

Or overload the method with void Minimum(); and in this method call the one who has an argument with a member.

 void Tree::Minimum() { Minimum(root); } 
+2
source

I could not find a way to make this default setting work like this. But you can get the same result by overloading the function, for example:

 class Tree { public: Node *root; Tree() { root=NULL; } void Minimum(Node*); void Minimum(); }; void Tree::Minimum(Node *curnode) { Node *parent; while(curnode!=NULL) { parent=curnode; curnode=curnode->leftchild; } std::cout<<parent->info<<std::endl; } void Tree::Minimum() { Minimum(root); } 
+1
source

If the case where the NULL argument NULL explicitly passed is not to be distinguished from the passed argument, you can set NULL as the default and use root if curnode is NULL .

 void Tree::Minimum(Node *curnode=NULL) { if (curnode==NULL) curnode = root; Node *parent; while(curnode!=NULL) { parent=curnode; curnode=curnode->leftchild; } std::cout<<parent->info<<endl; } 
0
source

Source: https://habr.com/ru/post/1498626/


All Articles