指向std :: string的空指针

编程入门 行业动态 更新时间:2024-10-21 19:27:22
本文介绍了指向std :: string的空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨! 我发现了一些有趣的东西。 我的意思是你怎么看待这个: char * p = 0; std :: string str = p; 为什么std :: string不检查空指针? 为什么不做一些检查: class String:public std :: string { public: String():std :: string(){} String(const char * str):std :: string() { if(str == 0) * this = std :: string(); else * this = std :: string(str); } String(const std :: string& str):std :: string(str){} }; 这只是一个例子,'因为我不想从头开始写 字符串类,所以我只是派生出来的。 :)

Hi! I just found something interesting. I mean what do you think about this: char *p = 0; std::string str = p; Why std::string doesn''t check null pointers? Why not doin'' some checking: class String : public std::string { public: String():std::string() {} String(const char *str):std::string() { if(str == 0) *this = std::string(); else *this = std::string(str); } String(const std::string& str):std::string(str) {} }; This is only an example, ''cause i didn''t want to write from scratch string class, so i just derived. :)

推荐答案

mr_sorcerer写道: mr_sorcerer wrote: 嗨! 我发现了一些有趣的东西。 我的意思是你怎么看待这个: char * p = 0 ; std :: string str = p; 为什么std :: string不检查空指针? Hi! I just found something interesting. I mean what do you think about this: char *p = 0; std::string str = p; Why std::string doesn''t check null pointers?

因为标准更喜欢未定义的行为浪费CPU周期。

Because the standard prefers undefined behavior to wasting CPU cycles.

为什么不这样做''有些检查: class String:public std :: string { public: String():std :: string(){} 字符串(const char * str):std :: string(){ if(str == 0) * this = std :: string(); else * this = std :: string(str); } String(const std :: string& str):std :: string(str){} }; 这只是一个例子,'因为我不想从头开始写 字符串类,所以我只是派生了。 :) Why not doin'' some checking: class String : public std::string { public: String():std::string() {} String(const char *str):std::string() { if(str == 0) *this = std::string(); else *this = std::string(str); } String(const std::string& str):std::string(str) {} }; This is only an example, ''cause i didn''t want to write from scratch string class, so i just derived. :)

随意这样做。我更喜欢断言(str!= 0)。这样,我将使用String进行测试,并且当我确信我成功避免UB时,可以用std :: string替换它。 顺便说一句:我发现有一个命名空间调试有点整洁,它提供了 debug :: vector<>,debug :: string,...具有已定义的行为(断言) )相反 of UB。 Best Kai-Uwe Bux

Feel free to do that. I would prefer an assert( str != 0 ). That way, I would use String for testing and could replace it with std::string when I am convinced that I avoided UB successfully. BTW: I find it is somewhat neat to have a namespace debug that provides debug::vector<>, debug::string, ... with defined behavior (assert) instead of UB. Best Kai-Uwe Bux

On Wed,04 Apr 2007 06:24:40 -0400,Kai-Uwe Bux写道: On Wed, 04 Apr 2007 06:24:40 -0400, Kai-Uwe Bux wrote: > mr_sorcerer写道: >mr_sorcerer wrote: >我刚发现了一些有趣的东西。我的意思是你怎么看待这个: char * p = 0; std :: string str = p; 为什么std :: string不检查空指针? >I just found something interesting.I mean what do you think about this:char *p = 0;std::string str = p;Why std::string doesn''t check null pointers?

因为标准更喜欢未定义的行为浪费CPU周期。

Because the standard prefers undefined behavior to wasting CPU cycles.

OTOH,公共成员函数应验证输入。我会考虑好的风格 。

OTOH, a public member function should validate input. I''d consider that good style.

>顺便说一句:我发现提供一个命名空间调试有点整洁 debug :: vector<>,debug :: string,...定义了行为(断言),而不是UB。 >BTW: I find it is somewhat neat to have a namespace debug that providesdebug::vector<>, debug::string, ... with defined behavior (assert) insteadof UB.

断言由NDEBUG #define启用/禁用。断言使用''命名空间 NDEBUG'';-) - Roland Pibinger "最好的软件简单,优雅,充满戏剧性。 - Grady Booch

assert is en-/disabled by the NDEBUG #define. assert uses ''namespace NDEBUG'' ;-) -- Roland Pibinger "The best software is simple, elegant, and full of drama" - Grady Booch

Roland Pibinger写道: Roland Pibinger wrote: 2007年4月4日星期三06:24:40 - 0400,Kai-Uwe Bux写道: On Wed, 04 Apr 2007 06:24:40 -0400, Kai-Uwe Bux wrote: >> mr_sorcerer写道: >>mr_sorcerer wrote: >> ;我刚发现了一些有趣的东西。我的意思是你怎么看待这个: char * p = 0; std :: string str = p; 为什么std :: string没有检查空指针? >>I just found something interesting.I mean what do you think about this:char *p = 0;std::string str = p;Why std::string doesn''t check null pointers?

因为标准更喜欢未定义的行为浪费CPU周期。

Because the standard prefers undefined behavior to wasting CPU cycles.

OTOH,公共成员函数应验证输入。我会考虑好的风格 。

OTOH, a public member function should validate input. I''d consider that good style.

是的,但这是一个实施质量问题。标准没有 要求标准容器的公共成员功能执行任何操作。 当然,您可以购买/实施然后使用标准 库实现,包括你想要的assert()语句。

True, but that is a quality of implementation issue. The standard does no require public member functions of standard containers to do any of that. Of course, you could just purchase / implement and then use a standard library implementation that includes the assert() statements you want.

>> BTW :我发现有一个命名空间调试有点整洁,它提供了 debug :: vector<>,debug :: string,...具有定义的行为(断言)而不是UB。 >>BTW: I find it is somewhat neat to have a namespace debug that providesdebug::vector<>, debug::string, ... with defined behavior (assert) insteadof UB.

断言由NDEBUG #define启用/禁用。断言使用''命名空间 NDEBUG'';-)

assert is en-/disabled by the NDEBUG #define. assert uses ''namespace NDEBUG'' ;-)

是的。但是,只有当您使用标准库 实现输入验证,范围检查等时才有效。 否则,您必须滚动自己的代码。 最好 Kai-Uwe Bux

Yes. However, that only works if you are using an standard library implementation that does input validation, range checking, and so on. Otherwise, you have to roll your own code. Best Kai-Uwe Bux

更多推荐

指向std :: string的空指针

本文发布于:2023-07-29 00:46:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   std   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!