c++ - C++中的引用指针

标签 c++

<分区>

我一直了解到,仅通过引用传递变量意味着该变量将保留对函数中变量所做的任何更改,并且函数的工作不会改变,但在这种情况下,函数的工作方式完全不同。 唯一的区别在于两个代码中这个函数的定义。 无效旋转(节点*&); 无效旋转(节点*);

这是第一个代码

#include<iostream>
#include<stack>
using namespace std;
struct node
{
    char data;
    node* left;
    node* right;
    node()
    {
        left=0;
        right=0;
    }
};
class AVL
{
private:
    node* root;
public:
    AVL()
    {
        root=NULL;
    }
    void initialize();
    void rotate(node*&);
    node*& return_n()
    {
        return root->left;
    }
    void traverse();

};
void AVL::initialize()
{
    root=new node;
    root->data='a';
    root->left=new node;
    root->left->data='b';
    root->left->left=new node;
    root->left->left->data='c';
    root->left->left->left=new node;
    root->left->left->left->data='d';
}
void AVL::traverse()
{
    node* temp=root;
    if(temp!=0)
    {
        stack<node*> s;
        s.push(temp);
        while(!s.empty())
        {
            temp=s.top();
            s.pop();
            cout<<endl<<temp->data<<endl;
            if(temp->left!=0)
            {
                s.push(temp->left);
                cout<<"Left: "<<s.top()->data<<endl;
            }
            else
            {
                cout<<"Left: NULL"<<endl;
            }
            if(temp->right!=0)
            {
                s.push(temp->right);
                cout<<"Right: "<<s.top()->data<<endl;
            }
            else
            {
                cout<<"Right: NULL ";
            }
        }
    }
}
void AVL::rotate(node*& temp)
{
    node* temp2=temp->left;
    cout<<endl<<"Step1"<<endl;
    cout<<endl<<"Temp"<<temp->data<<endl;
    cout<<endl<<"Temp2"<<temp2->data<<endl;
    traverse();
    temp->left=temp2->right;
    cout<<endl<<endl<<endl;
    cout<<endl<<"Step2"<<endl;
    cout<<endl<<"Temp"<<temp->data<<endl;
    cout<<endl<<"Temp2"<<temp2->data<<endl;
    traverse();
    temp2->right=temp;
    cout<<endl<<endl<<endl;
    cout<<endl<<"Step3"<<endl;
    cout<<endl<<"Temp"<<temp->data<<endl;
    cout<<endl<<"Temp2"<<temp2->data<<endl;
    traverse();
    temp=temp2;
    cout<<endl<<endl<<endl;
    cout<<endl<<"Step4"<<endl;
    cout<<endl<<"Temp"<<temp->data<<endl;
    cout<<endl<<"Temp2"<<temp2->data<<endl;
    traverse();
    system("pause");
}
int main()
{
    AVL obj;
    obj.initialize();
    obj.traverse();
    obj.rotate(obj.return_n());
    return 0;
}

这是第二个代码

#include<iostream>
#include<stack>
using namespace std;
struct node
{
    char data;
    node* left;
    node* right;
    node()
    {
        left=0;
        right=0;
    }
};
class AVL
{
private:
    node* root;
public:
    AVL()
    {
        root=NULL;
    }
    void initialize();
    void rotate(node*);
    node*& return_n()
    {
        return root->left;
    }
    void traverse();

};
void AVL::initialize()
{
    root=new node;
    root->data='a';
    root->left=new node;
    root->left->data='b';
    root->left->left=new node;
    root->left->left->data='c';
    root->left->left->left=new node;
    root->left->left->left->data='d';
}
void AVL::traverse()
{
    node* temp=root;
    if(temp!=0)
    {
        stack<node*> s;
        s.push(temp);
        while(!s.empty())
        {
            temp=s.top();
            s.pop();
            cout<<endl<<temp->data<<endl;
            if(temp->left!=0)
            {
                s.push(temp->left);
                cout<<"Left: "<<s.top()->data<<endl;
            }
            else
            {
                cout<<"Left: NULL"<<endl;
            }
            if(temp->right!=0)
            {
                s.push(temp->right);
                cout<<"Right: "<<s.top()->data<<endl;
            }
            else
            {
                cout<<"Right: NULL ";
            }
        }
    }
}
void AVL::rotate(node* temp)
{
    node* temp2=temp->left;
    cout<<endl<<"Step1"<<endl;
    cout<<endl<<"Temp"<<temp->data<<endl;
    cout<<endl<<"Temp2"<<temp2->data<<endl;
    traverse();
    temp->left=temp2->right;
    cout<<endl<<endl<<endl;
    cout<<endl<<"Step2"<<endl;
    cout<<endl<<"Temp"<<temp->data<<endl;
    cout<<endl<<"Temp2"<<temp2->data<<endl;
    traverse();
    temp2->right=temp;
    cout<<endl<<endl<<endl;
    cout<<endl<<"Step3"<<endl;
    cout<<endl<<"Temp"<<temp->data<<endl;
    cout<<endl<<"Temp2"<<temp2->data<<endl;
    traverse();
    temp=temp2;
    cout<<endl<<endl<<endl;
    cout<<endl<<"Step4"<<endl;
    cout<<endl<<"Temp"<<temp->data<<endl;
    cout<<endl<<"Temp2"<<temp2->data<<endl;
    traverse();
    system("pause");
}
int main()
{
    AVL obj;
    obj.initialize();
    obj.traverse();
    obj.rotate(obj.return_n());
    return 0;
}

最佳答案

通过引用传递 obj.return_n(),您可以为函数提供修改此引用的可能性。

在第一段代码中,当你遇到那些行时:

temp=temp2;
cout<<endl<<endl<<endl;
cout<<endl<<"Step4"<<endl;
cout<<endl<<"Temp"<<temp->data<<endl;
cout<<endl<<"Temp2"<<temp2->data<<endl;
traverse();

temp 是对 root->left 的引用,在 AVL::traverse() 中使用,在调用之前被修改到 traverse()。第二种情况,temp是一个拷贝,root->left修改的,那么traverse不表现得像以前一样。

因此有所不同。

您应该学会正确使用引用。您可以使用 const 关键字防止它们被修改。我要补充一点,对指针的引用看起来是一个糟糕的设计。

关于c++ - C++中的引用指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138028/

相关文章:

c++ - 赋值运算符复制数组中除可用空间以外的所有内容

c++ - 如何正确创建oauth签名基字符串?

c++ - 在 boost::lambda 中使用 static_cast

c++ - 我可以在没有 qmake 或 Qt Creator 的情况下使用 Qt 吗?

c++ - 如何压缩一个非重复数字大小为N位的序列?

c++ - Xcode 中多个图像之间的交集

c++ - 自动实例与 new - 创建对象 C++

c++ - 嵌入式 C++ 类交互

c++ - #define 在 C/C++ 中的用法

c++ - VTK 6.1 和 Qt 5.3 : 3D Objects in QVTKWidget with bad transparencies