博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++模板别名的理解
阅读量:7119 次
发布时间:2019-06-28

本文共 1283 字,大约阅读时间需要 4 分钟。

hot3.png

故事背景:

最近在看邓俊辉老师的书《数据结构(C++语言版》。不得不说这本书写的太好了,强烈推荐大家看看。

我以前也学过C++,基础的语法还是知道的,也知道C++里模板的用法。所以我满以为凭这点底子看这本书的示例代码应该是没问题的。我还特地找了一个C++在线编译器。这个在线编译器支持多种版本的C++语法,还支持多文件。

在看到第三章列表节点模板类的示例代码时,我看不懂了。代码是这样的:

typedef int Rank; //秩#define ListNodePosi(T) ListNode
* //列表节点位置template
struct ListNode { //列表节点模板类(以双向链表形式实现)// 成员T data; ListNodePosi(T) pred; ListNodePosi(T) succ; //数值、前驱、后继// 极造函数ListNode() {} //针对header和trailer的构造ListNode( T e, ListNodePosi(T) p = NULL, ListNodePosi(T) s = NULL) : data(e), pred(p), succ(s) {} //默认构造器// 操作接口ListNodePosi(T) insertAsPred(T const& e); //紧靠当前节点之前插入新节点ListNodePosi(T) insertAsSucc(T const& e); //紧随当前节点之后插入新节点};

看不懂的有两句(凭什么可以这样写?为什么我写不出来?):

#define ListNodePosi(T) ListNode<T>*

ListNodePosi(T) pred;

先来看看微软msdn对#define的语法解释:

#define Directive (C/C++)

#define identifier token-stringopt#define identifier ( identifieropt,...,identifieropt)token-stringopt

The #define directive causes the compiler to substitute token-string for each occurrence of identifier in the source file. The identifier is replaced only when it forms a token. That is, identifier is not replaced if it appears in a comment, in a string, or as part of a longer identifier. For more information, see .

转载于:https://my.oschina.net/u/3611008/blog/2933908

你可能感兴趣的文章
grub.conf加密
查看>>
WSFC时间分区场景实作
查看>>
linux中的邮件服务器笔记
查看>>
CCNP-18 IS-IS试验1(BSCI)
查看>>
Comet和WebSocket
查看>>
使用委托进行异步编程
查看>>
silverlight 跨域socket
查看>>
编程不是功能实现了就可以了
查看>>
Linux/Freebsd下时间转化
查看>>
微软MED-V虚拟化实战教程之一部署
查看>>
chmod和permission
查看>>
4-2 ADO.NET-查询和检索数据7
查看>>
Day7:html和css
查看>>
centOS7 安装Git
查看>>
超全的设计模式简介(45种)
查看>>
Modbus功能码
查看>>
spring cloud互联网分布式微服务云平台规划分析--spring cloud定时调度平台
查看>>
Visual Sudio 复制窗体文件
查看>>
Google Auth+openssh
查看>>
Windows 8 新启动方式:混合启动(Hybrid Boot)
查看>>