3
#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;
}

Can I use using instead of typedef in this situation?

I have searched some information on the web: Some people say using can replace typedef, but how can I replace this? (Any documentation or blog would also be helpful.)

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
yesung
  • 51
  • 5
  • 1
    `using pointer_to_pos_x = int Pos::*` – Peter Jan 17 '21 at 07:34
  • using is the same as typedef but with inverted order of types. I prefer using as it has the "natural" order and syntax as any other assignments. – Klaus Jan 17 '21 at 08:49

1 Answers1

2

Just this:

using pointer_to_pos_x = int Pos::*;

Virtually all cases of a typedef XXX aaa; can be converted readily to using aaa = XXX;. You may also find this Q/A useful: What is the difference between 'typedef' and 'using' in C++11?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83