C++:如何处理 NULL 值(例如来自数据库的值)?
在 C++ 中,如何在我们可能期望字符串或整数的上下文中处理 NULL 值?
通常,上下文是从数据库检索数据时,例如使用 LEFT JOIN,其中某些列中的某些不存在的值将由 NuLL 值而不是预期的字符串或整数表示。
C++ 中的强强制转换使得此类场景比 PHP 更难处理。
在 PHP 中,我们可以使用 === 运算符轻松实现 if/else 切换:
if ($value === NULL) {
// No data.
} else {
// We have some valid data.
}
在 C++ 中等效的情况会是什么样子?
我搜索过,但找不到 C++ 相关问题。
这个问题一般来说是有效的。在我的特定情况下,我使用 dbixx SQL 库。
In C++, how to deal with NULL values in a context where we might expect a string or an integer?
Typically, the context would be when retrieving data from a database, e.g. with a LEFT JOIN where some non-existing values in some column would be represented by a NuLL value instead of the expected string or integer.
Strong casting in C++ makes this type of scenario much more difficult to deal with than with PHP.
In PHP, we can easily have a if/else switch using the === operator:
if ($value === NULL) {
// No data.
} else {
// We have some valid data.
}
What would the equivalent look like in C++?
I've searched and couldn't find a C++ relevant question.
The question is valid in general. In my particular case, I am using the dbixx SQL library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
boost::可选
是在 C++ 中用强类型检查来表示可能类型的自然方式(必须检查可能为 null 的类型)boost::optional
is a natural way to represent maybe types in c++ with strong type checking (types that could be null must be checked for null)来自 http://art-blog.no-ip.info/wikipp/ en/page/ref_dbixx_row :
在你的问题中,你说你正在使用 dbixx,所以我链接到了他们的文档。这将始终特定于您正在使用的库。
From http://art-blog.no-ip.info/wikipp/en/page/ref_dbixx_row :
In your question you stated you were using dbixx, so I have linked to their documentation. This will always be specific to the library you are using.
这听起来像是一个通用概念,但 C++ 没有通用答案。该语言本身没有真正通用的“NULL”值,可以与所有类型一起使用以区分已设置为实际值。
因此,任何情况下的答案完全取决于库或您正在使用的任何内容以及它是如何设计来指示“NULL”值的。用于访问数据的函数的返回值可能指示数据是否为 NULL 或其他。或者可能有类似单独的 IsNull() 函数的东西。或者图书馆可能会使用其他一些不同的方案。有很多可能性。
唯一可能的一般答案是:阅读库的文档以了解他们具体如何处理它。
It may sound like a general concept, but there is no general answer for C++. The language itself does not have a truly general "NULL" value that can by used with all types to distinguish from having been set to an actual value.
So the answer in any situation entirely depends on the library or whatever that you're working with and how it has been designed to indicate "NULL" values. The return values of functions used to access the data may perhaps indicate whether the data is NULL or otherwise. Or there may be something like a separate IsNull() function. Or the library may use some other different scheme. There are many possibilities.
The only possible general answer is: read the documentation of the library to find out how they specifically deal with it.
对于字符串指针(以及一般的对象指针),很简单,它们可以保存 0 来表明它们为空。
对于整数,您必须依赖一个外部变量来判断返回的对象是否为 null。或者,如果将其作为指针传递,则可以像上面一样使用 0 指针。
For string pointers (and object pointers in general), it's easy, they can hold 0 to show that they're null.
For integers, you have to rely on an external variable that says if the object returned is null or not. Or if you pass it as a pointer, you can use a 0 pointer like above.