C++学习014函数值传递和地址传递

当我们给一个函数传参数的时候,可以直接值传入函数,也给可以把一个地址传入函数

 

区别就是一个本身不被改变,而另一本身也在改变,

 

在开发时候都会用到,

 

这里做下记录

 

 

#include <iostream>

using namespace std;


class Test
{
    public:
        Test(int a)
        {
            num = a;
        }
        void setnum(int a)
        {
            num =a;
        }
        int getnum()
        {
            return num;
        }
    private:
        int num;
};



void add(Test t)
{
    t.setnum(t.getnum()+t.getnum());
    cout<<"\nf num value add:";
    cout<<t.getnum()<<endl;
}

void add1(Test *t)
{
    t->setnum(t->getnum()+t->getnum());
    cout<<"\nf num value add:";
    cout<<t->getnum()<<endl;
}



int main()
{
    cout << "Hello world!" << endl;


    Test tt(100);
    add(tt);
    cout<<"object tt not chaange is main:";
    cout<<tt.getnum()<<endl;

    Test tt1(100);
    add1(&tt1);
    cout<<"object tt not chaange is main:";
    cout<<tt1.getnum()<<endl;





    return 0;
}
<img src="https://img-blog.csdn.net/20160815152346411?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


 

 

 

 

posted @ 2016-08-15 15:25  DreamDog  阅读(198)  评论(0编辑  收藏  举报