美文网首页C++ 11
003 类型别名

003 类型别名

作者: 赵者也 | 来源:发表于2020-02-02 14:53 被阅读0次

传统的定义别名的方法是使用关键字 typedef

typedef  double wages;  // wages 是 double 的同义词
typedef wages base, *p; // base 是 double 的同义词,p 是 double* 的同义词

新标准规定了一种新的方法,使用 别名声明 来定义类型的别名:

class CollegeStudents
{
public:
    CollegeStudents() {}
    // ...
};

using CS = CollegeStudents;

使用示例:

wages hours, weeks;
CS someone;

** 指针、常量和类型别名 **

如果某个类型别名指代的是复合类型或常量,那么把它用到声明语句里就会产生意想不到的后果。例如下面的声明语句用到了类型 pstring,它实际上是类型 char* 的别名:

typedef char * pstring;
const pstring nullstr0 = nullptr;
const pstring *ps;

上面两条声明语句的基本类型都是 const pstring,const 是对给定类型的修饰。pstring 实际上是指向 char 的指针,因此,const pstring 就是指向 char 的常量指针,而非指向常量字符的指针。
遇到一条使用了类型别名的声明语句时,乡亲们往往会错误地尝试把类型别名替换成它本来的样子,以理解该语句的含义:

const char * nullstr1 = nullptr;

然而,乡亲们,这种理解是错误的。声明语句中用到 pstring 时,其基本数据类型是指针。可是用 char * 重写了声明语句后,数据类型就变成了 char,* 成为了声明符的一部分。这样改写的结果是, const char 成了基本数据类型。前后两种声明含义截然不同,前者声明了一个指向 char 的常量指针,改写后的形式则声明了一个指向 const char 的指针。

使用如下代码测试 ps、nullstr0、nullstr1 的类型:

#include <QCoreApplication>
#include <cxxabi.h>
#include <QDebug>
#include <typeinfo>
#include <iostream>
#include <string>
#include <memory>
#include <cstdlib>

namespace  {

std::string demangle(const char* mangled)
{
      int status;
      std::unique_ptr<char[], void (*)(void*)> result(
        abi::__cxa_demangle(mangled, nullptr, nullptr, &status), std::free);
      return result.get() ? std::string(result.get()) : "error occurred";
}

template<class T>
void foo(T t) { std::cout << demangle(typeid(t).name()) << std::endl; }


typedef char * pstring;
const pstring nullstr0 = nullptr;
const pstring *ps;

const char * nullstr1 = nullptr;

}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    foo(ps);
    foo(nullstr0);
    foo(nullstr1);

    return a.exec();
}

输出结果为:

char* const*
char*
char const*

相关文章

  • 003 类型别名

    传统的定义别名的方法是使用关键字 typedef 新标准规定了一种新的方法,使用 别名声明 来定义类型的别名: 使...

  • flow中文文档(六)

    类型别名 类型别名语法 类型别名泛型 不透明类型别名 子类型约束 类型别名 当您有要在多个位置重用的复杂类型时,可...

  • TypeScript基础入门之高级类型的类型别名

    转发 TypeScript基础入门之高级类型的类型别名 高级类型 类型别名 类型别名会给一个类型起个新名字。 类型...

  • Flow不透明类型别名(Opaque Type Aliases)

    不透明类型别名(Opaque Type Aliases) 通过类型系统加强抽象 不透明类型别名是类型别名,它们不允...

  • 01_GO语言中级学习之类型(type)

    类型别名与类型定义 类型别名 以下代码使用的就是类型别名,例码: 类型定义 以下代码使用的就是类型定义,例码: 两...

  • V语言学习笔记-16类型别名

    type alias 类型别名 可以在某一个类型的基础上,定义类型别名 基于基本类型-定义类型别名 基于结构体类型...

  • 【进阶】TS高级类型,泛型

    # 类型别名 type 类型别名就是给已有的类型取一个新名字,并不会新建类型 类型别名:可以用于原始值,联合类型,...

  • TypeScript type/元组/枚举/类/泛型

    类型别名 type 类型别名用来给一个类型起个新名字。 类型别名常用于联合类型 字符串字面量类型 字符串字面量类型...

  • 每天学一点 Kotlin -- 对象进阶:类型别名

    1. 类型别名 1.1 类型别名:使用关键字 typealias 进行定义,具体格式如下: 1.2 类型设定别名和...

  • Typescript进阶

    类型别名 类型别名用来给一个类型起个新名字。 上例中,我们使用 type 创建类型别名。 字符串字面量类型 字符串...

网友评论

    本文标题:003 类型别名

    本文链接:https://www.haomeiwen.com/subject/glxueqtx.html