用Cstring替换LPCTSTR安全吗?

发布于 2025-01-25 13:42:07 字数 370 浏览 2 评论 0原文

功能的参数需要lpctstr type actible;如果我们传递cstring类型变量怎么办?在这种情况下,它是安全的还是我们应该考虑的东西?

例如,此功能:

Add(new CAToolbar, _T("Command Toolbar"), xtpBarTop);

在这里,我想用lpctstr type _T(“命令工具栏”)用cstring str;替换。这样的事情:

Add(new CAToolbar, str , xtpBarTop);

安全吗?

A parameter of function requires an LPCTSTR type variable; what if we pass a CString type variable? Is it safe or is there anything that we should look at in this case?

For example this function :

Add(new CAToolbar, _T("Command Toolbar"), xtpBarTop);

Here I want to replace _T("Command Toolbar") (LPCTSTR type) with CString str;. Something like this:

Add(new CAToolbar, str , xtpBarTop);

Is it safe ?

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

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

发布评论

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

评论(2

偏爱你一生 2025-02-01 13:42:07

它是安全的还是在这种情况下我们应该看什么?

在这种情况下, cstring类具有运算符lpctstr(),该将字符串的内容返回为c风格,nul-enul-terminated char> char或<代码> wchar_t 数组(嗯,是其第一个元素的指针),具体取决于“字符集”选项的当前编译器设置(即是否Unicode和/或_unicode是/定义)。因此,返回的类型将(或应该)匹配所需的const char* vs const wchar_t*指针类型用于您的功能参数。 (请注意,这是不是 可用于不是 const合格的功能参数的安全参数)。

在“旧时代”中,我使用了一些编译器,这些编译器会在执行此操作时抱怨'隐式'转换,因此我使用了cstring :: getString()函数。但是,这些确实 同样的事情。从“ atlsimpstr.h”标题:

template< typename BaseType , bool t_bMFCDLL = false>
class CSimpleStringT
{
//...
    operator PCXSTR() const throw()
    {
        return( m_pszData );
    }
//...
    PCXSTR GetString() const throw()
    {
        return( m_pszData );
    }
//...

重要说明:使用此“隐式转换”的一种情况是不是安全的安全是对 variadic griadic参数,例如printf.form> .format() cstring本身的成员;在这种情况下,编译器(正确)将(正确)抱怨非便携式使用类对象的使用。采用以下代码:

#include <afx.h>

int main()
{
    CString param = _T("param");
    CString text;
    text.Format(_T("Param is %s"), param);

    return 0;
}

Clang-CL编译器给出此错误:

错误:无法传递非平凡类型'cstring'的对象(又名
'cstringt&lt; wchar_t,tratraitmfc_dll&lt; wchar_t&gt;&gt;')通过variadic
方法;呼叫将在运行时流产[-wnon-pod-varargs]

,甚至MSVC本身也给予:

警告C4840:不可用的类
'atl :: cstringt&lt; wchar_t,trtraitmfc_dll&lt; wchar_t,atl :: chtraitscrt&lt; wchar_t;
作为variadic函数的参数

,您可以使用前面提到的.getString()函数,也可以进行转换 explicit

    text.Format(_T("Param is %s"), param.operator LPCTSTR());
// or ...
    text.Format(_T("Param is %s"), static_cast<LPCTSTR>(param));

Is it safe or is there anything that we should look at in this case?

Generally (see note, below), yes, it is safe. The CString class has an operator LPCTSTR(), which returns the content of the string as a C-style, nul-terminated char or wchar_t array (well, a pointer to its first element), depending on the current compiler setting of the "Character Set" option (i.e. whether or not UNICODE and/or _UNICODE is/are defined). Thus, the returned type will (or should) match the required const char* vs const wchar_t* pointer type for your function parameter. (Note that this is not safe to use for function parameters that are not const qualified).

In "olden days", there were some compilers that I used that would complain about the 'implicit' conversion when doing just this, so I used the CString::GetString() function, instead. However, these do exactly the same thing. From the "atlsimpstr.h" header:

template< typename BaseType , bool t_bMFCDLL = false>
class CSimpleStringT
{
//...
    operator PCXSTR() const throw()
    {
        return( m_pszData );
    }
//...
    PCXSTR GetString() const throw()
    {
        return( m_pszData );
    }
//...

Important Note: One case where using this "implicit conversion" is not safe is for functions that take variadic arguments, like printf or the .Format() member of CString itself; in such cases, the compiler will (rightly) complain of non-portable use of a class object. Take the following code:

#include <afx.h>

int main()
{
    CString param = _T("param");
    CString text;
    text.Format(_T("Param is %s"), param);

    return 0;
}

The clang-cl compiler gives this error:

error : cannot pass object of non-trivial type 'CString' (aka
'CStringT<wchar_t, StrTraitMFC_DLL<wchar_t>>') through variadic
method; call will abort at runtime [-Wnon-pod-varargs]

And even MSVC itself gives:

warning C4840: non-portable use of class
'ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>'
as an argument to a variadic function

In such a case, you can either use the .GetString() function mentioned earlier, or make the conversion explicit:

    text.Format(_T("Param is %s"), param.operator LPCTSTR());
// or ...
    text.Format(_T("Param is %s"), static_cast<LPCTSTR>(param));
还在原地等你 2025-02-01 13:42:07

cstring包含lpctstr的隐式转换操作员。它是为此精确使用而设计的。

这是您传递const字符串的事实,无法修改使其安全。

CString contains an implicit conversion operator for LPCTSTR. It was designed for this exact usage.

It's the fact that you're passing a const string that can't be modified that makes it safe.

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