【问题标题】:Can I use "using" instead of "typedef" for a pointer to class member variable? [duplicate]我可以使用“using”而不是“typedef”作为指向类成员变量的指针吗? [复制]
【发布时间】:2021-04-21 18:53:38
【问题描述】:
#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::*
  • using 与 typedef 相同,但类型倒序。我更喜欢使用它,因为它具有与任何其他分配一样“自然”的顺序和语法。

标签: c++ c++11 typedef using


【解决方案1】:

就这个:

using pointer_to_pos_x = int Pos::*;

几乎所有typedef XXX aaa; 的情况都可以轻松转换为using aaa = XXX;。您可能还会发现此 Q/A 有用:What is the difference between 'typedef' and 'using' in C++11?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多