Why am I getting an error when I try this in Visual C++?
struct S { };
typedef S *S;
Doesn’t C++ let you typedef a name that was previously only declared as a class or struct?
Or am I misunderstanding what’s going on?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
C++ does allow you to typedef an existing class name, but only in a very restricted way. Once you declared your
struct Syou can dobut you cannot do
or
In the first case the fact that you are redefining
Sas a pointer is what breaks it.The language specification says in 7.1.3/2
“Already refers” is the key part. In simple words, you can redefine a class name to stand for the same class type, but not for a pointer to the class type or anything else.
The aforementioned part of C++ standard is also what allows you to write repetitive typedefs like
which would be illegal in C.