小编bcx*_*000的帖子

在C++中检查空对象

我大部分时间只和C一起工作,并且在C++中遇到了一些不熟悉的问题.

假设我在C中有这样的函数,这是非常典型的:

int some_c_function(const char* var)
{
    if (var == NULL) {
        /* Exit early so we don't dereference a null pointer */
    }
    /* The rest of the code */
}
Run Code Online (Sandbox Code Playgroud)

让我们说我正在尝试用C++编写类似的函数:

int some_cpp_function(const some_object& str)
{
    if (str == NULL)  // This doesn't compile, probably because some_object doesn't overload the == operator

    if (&str == NULL) // This compiles, but it doesn't work, and does this even mean anything?
}
Run Code Online (Sandbox Code Playgroud)

基本上,我所要做的就是在使用NULL调用some_cpp_function()时防止程序崩溃.

  • 使用对象C++执行此操作的最典型/常用方法是什么(不涉及重载==运算符)?

  • 这甚至是正确的方法吗?也就是说,我不应该编写将对象作为参数的函数,而是编写成员函数吗?(但即使如此,请回答原始问题)

  • 在一个引用一个对象的函数或一个采用C风格指针指向一个对象的函数之间,是否有理由选择一个而不是另一个?

c++ null object

33
推荐指数
5
解决办法
13万
查看次数

通过C++程序在PowerShell中打印unicode字符

我的最终目标是通过C++程序将一些非拉丁文本输出写入Windows中的控制台.

cmd.exe让我无处可去,所以我得到了最新的,有光泽的PowerShell版本(支持unicode).我已经证实我可以

  • 输入非unicode字符和
  • 从windows命令看到非unicode控制台输出(如"dir")

例如,我有这个文件,"가.txt"(가是韩语字母表中的第一个字母),我可以得到这样的输出:

PS P:\reference\unicode> dir .\?.txt

    Directory: P:\reference\unicode

Mode                LastWriteTime     Length  
Name                                                       
----                -------------     ------ 
----                                                       
-a---         1/12/2010   8:54 AM          0 ?.txt     
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.但是使用C++程序写入控制台是行不通的.

int main()
{
    wchar_t text[] = {0xAC00, 0}; // ? has code point U+AC00 in unicode
    wprintf(L"%s", text);  // this prints a single question mark: "?"
}
Run Code Online (Sandbox Code Playgroud)

我不知道我错过了什么.我可以输入并在控制台上看到가的事实似乎表明我有三个需要的部分(unicode支持,字体和字形),但我必须弄错.

我也试过"chcp"而没有任何运气.我在C++程序中做错了吗?

谢谢!

unicode powershell

4
推荐指数
1
解决办法
2796
查看次数

标签 统计

c++ ×1

null ×1

object ×1

powershell ×1

unicode ×1