CString:(TCHAR*)(this + 1) 是什么意思?

发布于 2024-08-13 08:15:28 字数 614 浏览 15 评论 0原文

在 CString 头文件中(无论是 Microsoft 的还是开放基础类 - http://www. koders.com/cpp/fid035C2F57DD64DBF54840B7C00EA7105DFDAA0EBD.aspx#L77),有如下代码片段

struct CStringData
{   
    long nRefs;
    int nDataLength;
    int nAllocLength;
    TCHAR* data() { return (TCHAR*)(&this[1]); };
    ...
};

(TCHAR*)(&this[1])表示什么?

CStringData 结构体用于 CString 类 (http://www.koders.com/cpp/fid100CC41B9D5E1056ED98FA36228968320362C4C1.aspx)。

任何帮助表示赞赏。

In the CString header file (be it Microsoft's or Open Foundation Classes - http://www.koders.com/cpp/fid035C2F57DD64DBF54840B7C00EA7105DFDAA0EBD.aspx#L77 ), there is the following code snippet

struct CStringData
{   
    long nRefs;
    int nDataLength;
    int nAllocLength;
    TCHAR* data() { return (TCHAR*)(&this[1]); };
    ...
};

What does the (TCHAR*)(&this[1]) indicate?

The CStringData struct is used in the CString class (http :// www.koders.com/cpp/fid100CC41B9D5E1056ED98FA36228968320362C4C1.aspx).

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜血缘 2024-08-20 08:15:28

CString 有很多内部技巧,使其在传递给 printf 函数时看起来像普通字符串,尽管实际上是一个类 - 无需将其转换为 LPCTSTR > 在参数列表中,例如,在 printf 中的 varargs (...) 的情况下。因此,试图理解 CString 实现中的单个技巧或函数是个坏消息。 (数据函数是一个内部函数,它获取与字符串关联的“真实”缓冲区。)

有一本书 MFC Internals 介绍了它,IIRC Blaszczak 书可能会涉及它。

编辑:至于表达式在原始 C++ 中的实际翻译:-

TCHAR* data() { return (TCHAR*)(&this[1]); };

这表示“假装您实际上是一起分配的项目数组中的第一个条目。现在,第二个项目实际上不是 CString,它是 Unicode 或普通字符的普通 NUL 终止缓冲区 - 即 LPTSTR”。

表达同一件事的另一种方式是:

TCHAR* data() { return (TCHAR*)(this + 1); };

当您将 1 添加到指向 T 的指针时,实际上您根据原始内存地址添加了 1* sizeof T。因此,如果有一个位于 0x00000010 且 sizeof(CString) = 4 的 CString,则 data 将返回一个指向从 0x00000014 开始的 NUL 终止字符缓冲区数组的指针,

但是仅仅脱离上下文来理解这一点并不一定是一个好主意。

为什么您需要知道?

CString has lots of internal tricks which make it look like a normal string when passed e.g. to printf functions, despite actually being a class - without having to cast it to LPCTSTR in the argument list, e.g., in the case of varargs (...) in e.g. a printf. Thus trying to understand a single individual trick or function in the CString implementation is bad news. (The data function is an internal function which gets the 'real' buffer associated with the string.)

There's a book, MFC Internals that goes into it, and IIRC the Blaszczak book might touch it.

EDIT: As for what the expression actually translates to in terms of raw C++:-

TCHAR* data() { return (TCHAR*)(&this[1]); };

this says "pretend you're actually the first entry in an array of items allocated together. Now, the second item isnt actually a CString, it's a normal NUL terminated buffer of either Unicode or normal characters - i.e., an LPTSTR".

Another way of expressing the same thing is:

TCHAR* data() { return (TCHAR*)(this + 1); };

When you add 1 to a pointer to T, you actually add 1* sizeof T in terms of a raw memory address. So if one has a CString located at 0x00000010 with sizeof(CString) = 4, data will return a pointer to a NUL terminated array of chars buffer starting at 0x00000014

But just understanding this one thing out of context isnt necessarily a good idea.

Why do you need to know?

最单纯的乌龟 2024-08-20 08:15:28

它将紧邻 CStringData 结构之后的内存区域作为 TCHAR 字符数组返回。

如果您查看 CString.cpp 文件:

static const struct {
    CStringData data;
    TCHAR ch;
} str_empty = {{-1, 0, 0}, 0};

CStringData* pData = (CStringData*)mem_alloc(sizeof(CStringData) + size*sizeof(TCHAR));

It returns the memory area that is immediately after the CStringData structure as an array of TCHAR characters.

You can understand why they are doing this if you look at the CString.cpp file:

static const struct {
    CStringData data;
    TCHAR ch;
} str_empty = {{-1, 0, 0}, 0};

CStringData* pData = (CStringData*)mem_alloc(sizeof(CStringData) + size*sizeof(TCHAR));
听风念你 2024-08-20 08:15:28

他们这样做了,这样 CString 看起来就像一个普通的数据缓冲区,当你请求 getdata 时,它会跳过 CStringData 结构并直接指向真正的数据缓冲区,如 char*

They do this trick, so that CString looks like a normal data buffer, and when you ask for the getdata it skips the CStringData structure and points directly to the real data buffer like char*

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文