c++和c#关于对象赋值的一个重要差别

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

struct  haitao
{
public:
    int agee;
};

class guozhu{
public:
    guozhu(int a) :agee(a){}
    guozhu(){}
    int getAge(){ return agee; }
    void setAge(int aa){ agee = aa; }
private :
    int agee;
    string namee;
};

int _tmain(int argc, _TCHAR* argv[])
{
    guozhu tt(53);
    guozhu mm=tt;
    mm.setAge(99);
    cout << mm.getAge() <<"  "<< tt.getAge() << endl;
    getchar();
    return 0;
}

上面c++代码打印结果:
这里写图片描述



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Program
    {
        class guozhu
        {
            private int age;
            public guozhu() { }
            public guozhu(int a)
            {
                age = a;
            }

            public void setAge(int aa)
            {
                age = aa;
            }

            public int getAge()
            {
                return age;
            }
        }
        static void Main(string[] args)
        {
            guozhu tt = new guozhu(53);
            guozhu mm = tt;
            mm.setAge(99);
            Console.WriteLine(mm.getAge());
            Console.WriteLine(tt.getAge());
            Console.Read();
        }
    }
}

上面c#代码编译后执行的结果如下:
这里写图片描述



最后总结:
当对象A赋值给同一类型的对象B后,那么A和B的关系是:
C# :B引用A,对B修改,即对A进行修改
C++: 赋值时,A将自己拷贝一份给B,A和B之间是相互独立的,互不影响



FR:海涛高软(hunk Xu) QQ技术交流群:386476712

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值