C + + I want to use this (b)

Source: Internet
Author: User

What do you need to pay attention to from pure C environment to C + + environment?

Yes, although C + + has been claimed to be compatible with C, and many people even think C is a C + + subset, but C-brain residue must know that the two have a great difference!

The following points are more prominent, and later I add the other:

1. About Type conversions

C is weak type, at least less than C + + weak, under C Many types of conversion we do not care, the compiler will default, up to some nasty compiler (such as a soft home) will give a warning, but in C + + is not the same, these are error!! You do not show the declaration, do not think cross-type!! In fact, this is a good thing, more dozen letters, but the code is clear and rigorous:)

Example

In C, you write this:
  
int* p;
p = malloc (* sizeof (int));

In C + +, you have to write this:
  
int* p;
p = (int *) malloc (Ten * sizeof (int));

2. About character arrays

In C, you can declare an array of characters, which is exactly the length of the string, that is: there is no place to ' s ', but in C + + You cannot do this:

char s[3] = "abc"; ( correct in C, error in C + +).

My advice is: if you are a normal human being, you will also write in C:

Char s[4] = "ABC";

but in fact the best way to do this is: char s[] = "abc"; isn't it??

3. About nested type definitions

Example

struct S {
int A;
struct T {
int t;
} b;
int C;
Enum e {V1, V2} e;
};

struct T x;
Enum E y;

The above code in C no problem, this is because S, T, e Three of the scope (scope) is the same, but in C + +, is really wrong! In C + +, the scope of T and E is in S, want to use outside only s::t, which involves the problem of namespaces, we will say later.

But even in C, I will put E and t alone to define, this kind of nested writing, always have rushes feeling ~ ~

4. About enum types

We all know that in C the enumeration is the same as the integral type, and the enumeration is the integer, so we do this:

Enum RGB {red, green, blue} RGB;
++rgb;

Oh, look, how wonderful the world is. However, the world of C + + is brutal, enumerations are independent types, and the integer operators such as + + are not supported. So you say it doesn't matter:"rgb=rgb+1;" Just fine. It's a pity. It's wrong again. Yes, because there is no display of the type conversion, so the correct wording:

Enum RGB {red, green, blue} RGB;

RGB = RGB (RGB + 1);

Although some long, but fortunately, if your obsessive-compulsive disorder has reached the divine level, then there are ways to give the RGB Plus + + operator overload bar, the specific content later said.

5. About annotations

and/**/'s double annotation scheme has long been adopted by C, so there is nothing special to note, but you have to write:

i = j//* Comment */k;

So even God can not help you = =!

This time to write so much, next time to continue to add ~ ~

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.