我是C++编程的新手,但我有Java经验.我需要有关如何将对象传递给C++中的函数的指导.
我是否需要传递指针,引用或非指针和非引用值?我记得在Java中没有这样的问题,因为我们只传递了保存对象引用的变量.
如果您还可以解释在哪里使用这些选项,那将会很棒.
所以我完成了List练习并继续使用Binary Trees.我的代码到目前为止:
tree.h中
#include "Node.h"
class Tree
{
private:
int mCount;
Node *root;
public:
Tree();
~Tree();
void insert(int, Node *);
};
Run Code Online (Sandbox Code Playgroud)
Tree.cpp
void Tree::insert(int data, Node *node)
{
if( root == 0 )
{
Node *temp = new Node;
temp->setData(100);
temp->setRight(0);
temp->setLeft(0);
root = temp;
}
else
{
if( data > root->getData() )
return insert( data, root->getRight() );
else
return insert( data, root->getLeft() );
}
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
int main(int argc, char** argv)
{
Tree *tree = new Tree;
tree->insert( 100, …Run Code Online (Sandbox Code Playgroud)