Is it recommended to use typedef in C++?

Asked

Viewed 58 times

3

I know C++ is compatible with C, but C++ has its own way of programming and C as well, so there are legacy things from C that shouldn’t be used in C++, the typedef is one of them? If yes, what to use instead of the typedef when programming in C++?

  • typedef is compatible with c++ https://en.cppreference.com/w/cpp/language/typedef

1 answer

3


The typedef is one of the things C doesn’t use much in C++, but it depends on the situation.

The definition of a struct already is typedefed (every compound structure definition is already a type defined in the code) by C++ automatically, so there is no reason to use it in this situation, even if it is accepted by compatibility. The same goes for new language constructions that resemble.

And to create a alias type is recommended to adopt the using in place of typedef, at least in almost all situations. This is a more modern and better way.

  • What do you mean "typedefed"? It’s like if I write "struct minha_struct ms;" equals "my_struct ms;"? That’s it "typedefed"?

  • 1

    that’s right...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.