我已经获得了一些二叉搜索树的代码,并负责为其添加一些功能。但首先,我真的很想更好地理解给我的一个函数的参数/函数定义。编码:
void printTree( ostream & out = cout ) const
{
if( isEmpty( ) )
out << "Empty tree" << endl;
else
printTree( root, out );
}
void printTree( BinaryNode *t, ostream & out ) const
{
if( t != nullptr )
{
printTree( t->left, out );
out << t->element << endl;
printTree( t->right, out );
}
}
首先,我不明白为什么const函数声明末尾的括号后面有一个。对我来说没有意义的另一件事是第一个函数声明的参数 ostream & out = cout。为什么参数= 的东西,我从来没有见过这个。我不明白ostream & out一般是指什么。不带参数运行printTree()就可以了。为什么即使没有printTree没有参数的函数声明也能工作?
顺便说一句,这一切都在 C++ 中。