问题描述
#include <iostream>
using namespace std;
struct Pos {
int x;
float y;
};
typedef int Pos::* pointer_to_pos_x;
//using pointer_to_pos_x = ???;
int main()
{
Pos pos;
pointer_to_pos_x a = &Pos::x;
pos.*a = 100;
cout << pos.x << endl;
}
在这种情况下,我可以使用 using
代替 typedef
吗?
我在网上查了一些资料:有人说using
可以代替typedef
,但是这个怎么代替呢? (任何文档或博客也会有所帮助。)
解决方法
就这样:
using pointer_to_pos_x = int Pos::*;
几乎所有 typedef XXX aaa;
的情况都可以轻松转换为 using aaa = XXX;
。您可能还会发现此问答很有用:What is the difference between 'typedef' and 'using' in C++11?