C ++ _ typedef _ collection and sorting of difficult definitions

Source: Internet
Author: User

So far, we have met two strange definitions of typedef, so we decided to take notes for future use.

[Const pointer of typedef]

[Case]

Typedef string * pstr;

Const pstr cpstr;

What kind of pointer is cpstr in the above Code?

[Analysis]

Many of my friends, including myself, first reflected that cpstr was defined as a common pointer pointing to the const string object for a simple reason, replace string * And pstr.

// This is obviously a normal pointer to the const string object

Const string * cpstr;

However, cpstr is defined as a const pointer pointing to the string type.

I was a little surprised when I saw this answer, and I was surprised by the fact that it was the internal cause.

The error is that we can just replace it. The word "replace" should not be used in typedef, but in # define Macro. typedef defines a type alias, the results of the definition follow any standard defined normally. So how does this typedef define a const pointer?

First, analyze

Const pstr cpstr;

This Code defines a pstr-type const cpstr, which is actually

Pstr const cpstr;

For ease of understanding, take the int definition as an example.

// The two are equivalent. The former is a habit, and the latter is a standard.

Const int ival = 0;

Int const ival = 0;

Since cpstr is a const pstr object, what is pstr? Pstr is a string *, so cpstr becomes a const string * type object, that is, cpstr is a const pointer to a common string.

  Summary]

This misunderstanding mainly stems from the assumption that typedef is equivalent to the # define macro definition, so you can simply replace the characters. Actually, it is not. We will see this definition later. The simplest way is to start with the definition of this variable.

Typedef string * pstr;

Const pstr cpstr;

Think about the following steps (skilled:

① Cpstr is a constant of the pstr type.

② What is pstr?

③ Pstr is string *

④ Therefore, cpstr is a constant of the string type *, that is, string * const cpstr

[Function type of typedef]

[Case]

Typedef int FUNC (int );

What type does typedef define?

[Analysis]

Don't be surprised. In this case, it is neither a function pointer nor an error.

It defines a function type FUNC, which requires an int parameter and returns an int result.

[Summary]

FUNC TestFunc (func f );

The usage of FUNC is limited. The preceding statements may cause compilation errors.

The above Code uses FUNC twice, and the first error occurs, that is, the return type of the FUNC type function is wrong.

FUNC is a function type. In another function, a function type variable can only be used as a form parameter of another function, rather than a return type.

When FUNC is used as a parameter, the compiler automatically converts it to FUNC */* function pointer */, and the compiler does not convert FUNC as the function return type, therefore, an error occurs during compilation, which can be corrected as follows:

FUNC * TestFunc (func f );

Declared a function TestFunc, which requires a function F with the function type of FUNC as the form parameter and returns a function pointer of the FUNC type *

[Typedef and # define]

[Case]

Typedef int * Tpi;

# Define int * Dpi;

Tpi p1, p2;

Dpi p3, p4;

Why are the four variables defined in the above Code of different types?

[Analysis]

To distinguish the types of four variables: p1, p2, p3, and p4, you must first understand the difference between typedef and # define Macro. typedef is used to give aliases of existing types, # define is a simple replacement of characters. The above code is equivalent

Int * p1, * p2; // role of typedef

Int * p3, p4; // # define

That is to say, p3 is defined as an integer pointer, while p4 is only an integer.

  [Summary]

You need to know the differences between typedef and # define to prevent these subtle traps and ensure high-quality code.

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.