Comparison between struct in C language and C ++ Language

Source: Internet
Author: User

Comparison between struct in C language and C ++ Language

Author: stonehat

1. the C ++ language treats struct as a class, so the struct of C ++ can contain everything of the C ++ class, such as constructor, destructor, and youyuan, the only difference between the struct and C ++ classes in C ++ is

Struct members are public by default, and C ++ is private by default. The C language struct is not a class. It cannot have functions or use class features, such as public keywords or static keywords.

2. In C ++ struct, the initialization form of the members is the same as that of the class. You Cannot initialize the members directly, that is, You Cannot initialize the members at the same time. A type (such as int) in the middle of the C language struct cannot be directly initialized.

That is to say, the following int a = 0; in C ++ and C, it cannot pass.

struct HE
{
int a = 0;
}
// C ++ and C compilation cannot be passed.

3. Initialization Method: C ++ struct initialization. C ++ not only has the C language initialization method. As follows:

 

Struct HE
{
Int;

};
Int main (){
HE d = {3}; // because HE is a class name in C ++, struct can be omitted. Struct must be added to the C language.
Return 0;
}

You can also use the class initialization method.

struct HE
{
int a;

};
int main(){
HE d;
d.a = 3;
return 0;
}

4. For different types, the following code:

 

struct HE
{
int a;

};

In C, the actual data type is (struct HE), so defining a structure HE variable must carry struct.

Struct HE a; // C variable Mode

In C ++, the actual data type is HE (mainly because the struct is treated as a class). Therefore, struct is not required to define variables.

HE a; // C ++ language variable

5. typedef struct has the same functions in C ++ and C, and defines aliases. Note the differences between the following codes.

// Code 1

# Include <stdio. h>
# Include <stdlib. h>
Typedef struct HE
{
Int;

} * PHE, DHE, and EHE; // This defines a struct HE * type alias and two common aliases. The main function only considers pointer aliases.
Int main (){
PHE a = (PHE) malloc (sizeof (struct HE); // This is written in C Language
// PHE a = new HE (); this is written in C ++.
Return 0;
}
// Code 2
# Include <stdio. h>
# Include <stdlib. h>
Struct HE
{
Int;

} * PHE, DHE, EHE; // a pointer variable and two common variables are defined here.

The above two codes can omit the struct name HE.

Conclusion: The C ++ language is compatible with the C language, so the C language syntax won't go wrong in the C ++ compiler, but it is best for programmers to know what C language is, what is C ++.

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.