- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在 C++/CLI 项目中,我在 native C++ 类中有一个方法,我想检查 NULL
或 nullptr
gcroot
引用。我该怎么做呢?以下似乎都不起作用:
void Foo::doIt(gcroot<System::String^> aString)
{
// This seems straightforward, but does not work
if (aString == nullptr)
{
// compiler error C2088: '==': illegal for struct
}
// Worth a try, but does not work either
if (aString == NULL)
{
// compiler error C2678: binary '==' : no operator found
// which takes a left-hand operand of type 'gcroot<T>'
// (or there is no acceptable conversion)
}
// Desperate, but same result as above
if (aString == gcroot<System::String^>(nullptr))
{
// compiler error C2678: binary '==' : no operator found
// which takes a left-hand operand of type 'gcroot<T>'
// (or there is no acceptable conversion)
}
}
编辑
以上只是一个简化的例子。我实际上正在开发一个在托管代码和 native 代码之间“转换”的包装库。我正在研究的类是包装托管对象的 native C++ 类。在 native C++ 类的构造函数中,我得到一个 gcroot
引用,我想检查它是否为空。
最佳答案
使用 static_cast
将 gcroot
转换为托管类型,然后将其与 nullptr
进行比较。
我的测试程序:
int main(array<System::String ^> ^args)
{
gcroot<System::String^> aString;
if (static_cast<String^>(aString) == nullptr)
{
Debug::WriteLine("aString == nullptr");
}
aString = "foo";
if (static_cast<String^>(aString) != nullptr)
{
Debug::WriteLine("aString != nullptr");
}
return 0;
}
结果:
aString == nullptraString != nullptr
关于c++ - 如何测试 NULL 或 nullptr 的 gcroot 引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21239090/
我正在尝试为我的学校作业制作信息亭程序。当我编译它时,它不会发出任何错误信号。但是当我尝试初始化我的队列列表时,它说我不能使用 nullptr...我不明白我的代码有什么问题。 typedef str
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 4 年前。 Improve
这个问题在这里已经有了答案: Placement of the asterisk in pointer declarations (14 个答案) 关闭 4 年前。 在博客和论坛上,我注意到人们使用
为什么std::string str(nullptr)和 std::string str=nullptr是错的?有人可以详细解释原因吗? 最佳答案 这样的例子编译是因为存在一个 std::string
这个定义有效: const auto &b{nullptr}; 虽然失败: auto *b{nullptr}; 我尝试在 Visual C++、GCC 和 Clang 中编译它。他们都提示“无法推断类
刚刚看了一篇关于C++0x标准的文章:http://www.softwarequalityconnection.com/2011/06/the-biggest-changes-in-c11-and-w
我已将节点指针初始化为 nullptr 并将其作为引用传递给辅助函数。在辅助函数内,我将之前为 nullptr 的指针设置为等于 new Pointer。然而,函数结束后,它又被设置为nullptr。
有谁知道为什么分配type* = 0无效,而分配type* = nullptr无效呢?在这两种情况下typedef void type。谢谢 #include #include template
我正在尝试为类(class)项目创建链表。我的节点类有一个指向链接节点的指针和另一个指向专门书籍类的指针。 class Node{ private: Book *data; Node
我在destroyStudent()函数中删除指针aStudent,然后我将aStudent设置为空指针。但是,运行该函数后,aStudent 不再设置为nullptr,所以我必须再次将其设置为nul
我正在尝试制作一个基本的 HashMap。在将元素插入索引之前,我正在检查它是否存在于索引中。当我插入第一个元素时,它说该位置已经存在一个元素。我已经通过调试器,我的所有值都符合预期,map[hash
我正在尝试做类似的事情: if (foo) return SET_ERROR_AND_RETURN_NULL(ERROR_HERE); 使用... #define SET_ERROR_AND
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
我有一个看起来像这样的结构: struct Wolf { Wolf *dog; Wolf *puppy; }; 我写过: Wolf *alphawolf = new Wolf; 当我尝
我正在使用带有 Visual Studio 2012 的 C++(11)。 我使用定制的包装器类创建窗口。 CUIWindow* winA = new CUIWindow ( NULL, TEXT("
我为它编写了一个 PriorityQueue 类和一个迭代器类。但我不明白为什么这些行编译? PriorityQueue pq; auto z =pq.begin(); z=nullptr
我正在学习 enable_if 的用法我偶然发现了以下代码。 template ::value, T>::type* = nullpt
我有一个函数func(int a, char b, void *ptr)。第三个参数保留供内部使用,它应该是当前版本的 nullptr。有没有办法在函数原型(prototype)而不是定义中强制执行此
所以当我在学校编译它时 nullptr 错误没有出现,我想我可以通过在编译时添加一行来修复它,有没有另一种方法来摆脱它,另外两个错误我不明白为什么我要得到它们。有人可以至少解释一下 nullptr 错
这个问题在这里已经有了答案: Checking if this is null (8 个答案) 关闭 4 年前。 我想像这样替换代码: if ((obj != nullptr) && obj->is
我是一名优秀的程序员,十分优秀!