这是一小段代码:
#include <iostream>
using namespace std;
#define myptr int *
int main(){
myptr p,q;
int c;
p = &c;
q = &c;
c = 2;
cout<<c<<endl;
cout<<*p<<endl;
cout<<*q<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
p有类型int*而q有型int.为什么会这样?
宏替换后,myptr p,q;变为
int *p,q;
Run Code Online (Sandbox Code Playgroud)
认为是的类型q是一个常见的错误int*,但事实上它是int.
你需要的是typedef:
typedef int* myptr;
Run Code Online (Sandbox Code Playgroud)
将*结合到变量,而不是类型.您需要为该行上的每个声明重复它.
typedef会以您期望的方式工作:
typedef int * myptr;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
248 次 |
| 最近记录: |