C++

Source: Internet
Author: User
Tags constant requires

So far there have been two definitions of TypeDef, so decided to write them down for later use.

"The const pointer of a typedef"

Case

typedef string * PSTR;

Const PSTR CPSTR;

Q What kind of pointer is cpstr in the above code?

Analysis

A lot of my friends, including myself. When I first came in contact with it, the initial reaction was: CPSTR is defined as a normal pointer to a const string object, for the simple reason that replacing the position of string* and Pstr gets

This is obviously a normal pointer to a const string object

const string * CPSTR;

However, the CPSTR is defined as a const pointer to a string type, rather than a person's wish.

See this answer, compared to a lot of friends slightly surprised, I also so, which internal cause, and listen to my slow way.

The reason for this error is that we "replace it," and the word "permutation" should not be used in a TypeDef, but in #define macros, a typedef defines a type alias, and the result of the definition follows any standard defined by any normal definition. So how exactly does this typedef define a const pointer?

First analyze

Const PSTR CPSTR;

This code defines a PSTR type of const CPSTR, which is actually

pstr Const CPSTR;

For the sake of understanding, take the int definition to say

The two are equivalent, the former is customary, the latter is the standard rule

const int ival = 0;

int const IVAL = 0;

Now that we understand that CPSTR is a const PSTR type of object, what is PSTR? Pstr is string*, so it becomes cpstr a const string* type of object, or CPSTR is a const pointer to a normal string.

  " Summary"

This misunderstanding stems from the idea that a typedef is equivalent to a #define macro definition, and that a simple substitution character is possible. In fact, otherwise. The easiest way to see this definition in the future is to start by defining the variable

typedef string * PSTR;

Const PSTR CPSTR;

Follow the steps (proficiency is quick) think:

①cpstr is a constant of type pstr

What is ②PSTR?

③pstr is string*.

④ So, Cpstr is a constant of type string*, or string *const cpstr

"function type of typedef"

Case

typedef int FUNC (int);

What type does the typedef define?

Analysis

Don't be surprised, that's it, it's not a function pointer, and I'm not writing it wrong.

It defines a function type Func, the function type requires an int parameter, and returns an int result.

Summary

FUNC TestFunc (FUNC F);

The use of Func is limited, and the above statement causes a compile-time error.

The above code uses two times func, the first error is the Func type of function return type is wrong.

Func is a function type, in another function, a function type variable can be used only as a formal parameter of another function, not as a return type.

When Func is used as a formal parameter, the compiler automatically converts it to the func*//function pointer, and the compiler does not convert Func as a function return type, resulting in a compile-time error that can be amended as follows

func* TestFunc (FUNC F);

Declares a function testfunc, which requires a function of type Func as a formal parameter and returns a function pointer of a func type func*

"TypeDef and #define"

Case

typedef int* TPI;

#define INT* Dpi;

Tpi P1,P2;

Dpi P3,P4;

Ask the four variables defined in the above code, why are they different?

"Analysis"

To differentiate between the types of P1,P2,P3,P4 four variables, first understand the difference between a typedef and a #define macro, a typedef is an alias to an existing type, and a #define is just a simple character replacement. So the code above is equivalent to

The role of int *p1,*p2;//typedef

int *p3,p4;//#define的作用

That is, P3 is defined as an integer pointer, and P4 is just an integral type.

  Summary

Need to master the difference between typedef and #define, in case these subtle traps, for high-quality code to do well.

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.