#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