The differences between typedef struct and struct in C + + __c++

Source: Internet
Author: User

typedef struct should be a C language design is not very reasonable place, the following explanation is still relatively clear.

Notice different in C and C + +
In C, you define a struct type with a typedef:
typedef struct STUDENT
{
int A;
}stu;

Stu stu1;

struct Student stu1;


So when you declare a variable, you can: Stu stu1;

( If there is no TypeDef, you must use struct Student stu1; to declare)


The Stu here is actually the alias of struct student. Stu==struct Student

////////////////////////////////////////////////////////////////////////////////////////////////////////////
In addition here also can not write Student (so also cannot struct Student stu1;, must be Stu stu1;)
typedef struct
{
int A;
}stu;

Stu stu1;

////////////////////////////////////////////////////////////////////////////////////////////////////////////


But in C + + is very simple, direct
struct Student
{
int A;
};

Student STU2;
Then the structure body type student is defined, and the STU2 is directly student when the variable is declared.


In C + +, if you use a TypeDef, it makes a difference:
struct Student
{
int A;
}STU1;//STU1 is a variable

stu1.a;


typedef struct STUDENT2
{
int A;
}STU2;//STU2 is a structural body type =struct Student

STU2 S2;

s2.a;


Direct access to stu1.a when used
But STU2 must first stu2 the S2;
Then s2.a=10;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.