What is the difference between struct and typedef struct in C++?
Hello! I'm a beginner in C++ and switched from C. In C, I often used `typedef struct` to avoid writing the keyword `struct` every time when declaring a variable. For example:
```c
// C code
typedef struct {
int x;
int y;
} Point;
Point p; // works without 'struct'
```
Now I'm learning C++ and I see that many people simply write:
```cpp
struct Point {
int x;
int y;
};
Point p; // also works!
```
I'm confused: in C++, do I need to use `typedef struct` or can I just use `struct`? What is the difference between them? Are there situations where `typedef struct` is still useful? Or is it just a leftover from C?
I tried both options in Visual Studio 2022, and both compile. But I want to understand which option is considered correct and why. Thanks in advance for the explanation!
No answers yet.
Log in to leave an answer
Blogs
Top Mistakes of Beginner C# Developers and How to Avoid Them