Relationship between lpctstr and cstring

Source: Internet
Author: User
Type understanding

Lpctstr type:

L indicates the long pointer, which is left behind by Windows 3.1 and other 16-bit operating systems. In Win32 and other 32-bit operating systems, long pointers, near pointers, and far modifiers are designed for compatibility. No practical significance.

P indicates that this is a pointer.

C Indicates a constant.

T indicates that there is a _ t macro In the Win32 environment.

STR indicates that this variable is a string description. This macro is used to indicate whether your character uses Unicode,
If your program defines Unicode or other related macros, the character or string will be treated as a unicode string, otherwise it will be a standard ANSI string.

Therefore, lpctstr indicates a string that points to a fixed address and can change the semantics according to some macro definitions.

In the program, we need to use the T-type definition most of the time.

Lpctstr = const tchar *

Cstring and lpctstr are common. The reason is that the automatic type conversion defined by cstring is nothing special, and the simplest C ++ operator is heavy load.

The distinction between constant string ANSI and Unicode is determined by macro _ T. However, when _ T ("ABCD") is used, the string "ABCD" determines whether it is char * or w_char * based on whether it is specified as _ Unicode during compilation *. Similarly, tchar is the same as the target macro. You can see the definition.

For the sake of simplicity, we will only introduce ANSI, Unicode, and so on.

In ANSI, The lpctstr is a const char *, which is a constant string (which cannot be modified ).

Lptstr is a char *, that is, a common string (very large, modifiable ).

Both are basic types, and cstring is C ++ class. compatible with these two basic types is the minimum task.

Since const char * is the simplest (constant, does not involve Memory changes, and the operation is fast), cstring directly defines a type conversion function:

Operator lpctstr ()

{.

.....

}

The function returns the maintained string.

When you need a const char * and the cstring is passed in, the C ++ compiler automatically calls the cstring-heavy operator lpctstr () for implicit type conversion.

When cstring is required and const char * is passed in (in fact, char * can also be used), the C ++ compiler automatically calls the cstring constructor to construct a temporary cstring object.

Therefore, cstring and lpctstr are common.

But lptstr is different. It is char *, which means you can modify the data in it at any time. This requires memory management. (For example, if the string gets longer, the original storage space is not enough, you need to re-adjust the allocated memory ).

Therefore, you cannot forcibly convert const char * To char.

For example:

Lpstr = (lpstr) (lpctstr) string;

This is an insecure method.

In this case, forced type conversion is used, and you have forced conversion. The C ++ compiler certainly won't reject you, but he also thinks that you do know what you want to do. Therefore, no warning is given.

Forced conversion of any type is a powerful feature of C (++), but it is also a major drawback. This problem is gradually improved in Versions later than vc6 (for VC only) (you need a more explicit type conversion Statement ).

In fact, we can see the usage of string like lpstr = (lpstr) (lpctstr); in many places. This is generally because the constraints of the function are not well defined, for example, if a function accepts the input of a string parameter without any modification, the parameter should be defined as const char *, but many beginners cannot understand the usage of Const, or it is lazy. In short, it is written as char * at will *. In this way, the cstring must be forcibly converted.

This method is not safe and is not recommended. You must fully understand and confirm that the string is not modified.

Cstring to lptstr (char *). The predefined method is to call the getbuffer function of cstring, after use, you generally need to call the releasebuffer function to confirm the modification (in some cases, releasebuffer is not called, and you need to be very clear why this can be done in such a way, the general application environment does not consider this situation ).

At the same time, it should be noted that, between getbuffer and releasebuffer, the memory allocated by cstring is handled by you, so you cannot call other cstring functions.

Cstring to lpctstr:

Cstring CSTR;

Const char * maid = (lpctstr) CSTR;

To convert the string type to the cstring type:

Maid;

Cstring CSTR = lpctstr; post: http://vopit.blog.51cto.com/2400931/442156

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.