C + +: Aliases of types and aliases of objects

Source: Internet
Author: User

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"); }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.