'What role is typedef playing in this block of code?
I'm new to learning C++ and wanted some clarification on what typedef is doing in the block of code below.
typedef struct strNode Node;
struct strNode
{
int number;
int weight;
list<Node> edges;
};
I understand that struct strNode is creating a new datatype but what is typedef struct strNode Node doing exactly?
Solution 1:[1]
what is
typedef struct strNode Nodedoing exactly?
It's creating an alias for strNode so that you can use strNode and Node interchangeably. This setup is usually used in C to be able to use the alias within the struct definition:
typedef struct Node Node;
struct Node
{
int number;
int weight;
Node *next;
};
This is not needed in C++. Node could here be used directly without typedef:
struct Node
{
int number;
int weight;
std::list<Node> edges;
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ted Lyngmo |
