As we mentioned in the previous article, references are actually an alias for an object. We know that objects are materialized instances of types, so can types have aliases? The answer is yes.
#include <iostream>
using namespace std;
class human{
public:
void Talk();
~human(){cout<<"析构函数在工作..."<<endl;}
private:
int age;
};
void human::Talk(){
cout<<"Hello"<<endl;
}
int main()
{
typedef human people;
//这是定义一个类型别名,或者叫做同义词。human是原类型,people是新类型
people p;
p.Talk();
human &b=p;//这是定义一个对象别名,引用了p这个对象
b.Talk();
return 0;
}
and the TypeDef key in C # does not correspond to the implementation, to set the alias to the type, C # is generally the following approach
using System;
using System.Collections.Generic;
using System.Text;
using people = CSharpProject.Human;
namespace CSharpProject
{
class Program
{
static void Main(string[] args)
{
people p = new Human();
p.Age = 50;
p.Talk();
Console.Read();
}
}
class Human {
public int Age { get; set; }
public void Talk() { Console.WriteLine("Hello,world"); }
}
}