c++ - Can I use a member element as the default argument for a method of the class? -


the method minimum returns minimum element in binary search tree. if no argument passed prints minimum of calling object. if address of node passed prints minimum of subtree root node

when compiled shows "invalid use of non static data member tree::root"

#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; } 

no, cannot.

for default value can use either value, variable or function accessible in context of function definition is, in class definition, outside of particular object's context.

it helps me thinking on how compiler processes this. in particular, when compiler overload resolution function , finds overload has more arguments used @ place of call, compiler generate code @ place of call fill in rest of arguments. generated code always generate call all of arguments:

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

when compiler processes [1] , overload resolution finds void ::f(int x = g()) best candidate , picks up. fills default argument , generates call you:

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

if consider call member function, or member variable of class, not make sense in context of caller (the language changed adapt this, not impossible handle that, current model not work).


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -