CString a = "你好"; + “世界!”;是否可以?
我正在制作自己的字符串 class
并且我想确保 CString a = "Hello " + "World!";
有效(即不给编译器错误例如:无法添加 2 个指针
)。
我的字符串 class
在需要时自动转换为 char*
,因此编写 printf(a)
不会破坏代码。
有什么方法可以替换 字符 周围的编译器行为吗? (即在引号之间,"abc"
)。或者,更改 +
运算符的行为来处理字符串?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能完全这样做,因为您只能重载类类型的运算符,而字符串文字没有类类型。
您可以利用预处理期间发生的字符串文字串联:
或者您可以创建一个
CString
临时对象并附加到它:或者您可以有一个
CString
构造函数需要多个参数:You can't do exactly that because you can only overload operators for class types, and a string literal does not have class type.
You can take advantage of the concatenation of string literals that takes place during preprocessing:
or you can create a
CString
temporary and append to it:or you could have a
CString
constructor that takes more than one argument:正确答案
是No。
您不能重载内置运算符,例如指针。
James McNellis 已经回答了这个问题,所以我不再详细说明。
一个可能的替代方案...
(我使用 std::string 因为我没有关于你的内部字符串的信息)
使用 typedef 会给你的语法添加一些糖:
但是,我不会用两个字符的符号污染全局命名空间只是为了一点点糖...据我所知,代码效率很低(创建了两个字符串对象,加上一个临时对象,不能保证编译器会将其全部优化掉.. .)
出于好奇......出于
好奇,通过将字符串包装成一个薄类,您可以“添加”这两个指针。
首先,让我们创建包装器:
如您所见,它都是内联的,并且不会执行任何操作...不过,它能够将自身转换为 const char * 指针(这种 hack 很危险,所以请确保它是您想要的)想做)。
然后,对于这个包装器,让我们重载加法运算符:
现在,让我们使用包装器编写一个
main
函数,为便于使用而进行了类型定义:它编译并给出以下结果:
免责声明:我只是想尝试一下你的问题给我的想法,并与你分享。不要仅仅因为可以就应用此类代码。事实上,在使用这段代码之前就应该对其进行改进,以有效地覆盖所有情况,即使如此,一个简单的
typedef std::string S_ ;
恕我直言,会更好。AFAIK,我不会使用它,因为我对当前的 STL API 很满意。
那么 C++0x 呢?
在 C++0x 中,您将能够创建自己的文字。代码有点像:
您将这样使用它:
有关更多信息,请参阅以下问题:用户定义的文字为 C++ 添加了哪些新功能?。
The right answer
No.
You cannot overload operators for built-ins, such as pointers.
James McNellis already answered the question, so I won't elaborate.
A possible alternative...
(I use std::string because I have no info on your in-house string)
Using a typedef will add some sugar to your syntax:
But then, I wouldn't pollute the global namespace with a two-characters symbol just for a little sugar... And as far as I know, the code is inefficient (two string objects created, plus one temporary, without guarantee the compiler will optimize it all away...)
For curiosity's sake...
As a curiosity, by wrapping the string into a thin class, you can "add" those two pointers.
First, let's create the wrapper :
As you can see, it's both inlined, and will do nothing... Still, it's able to cast itself into a const char * pointer (this kind of hack is dangerous, so be sure it's what you want to do).
Then, for this wrapper, let's overload the addition operator :
And now, let's write a
main
function using the wrapper, typedefed for ease of use :Which compiles and gives the following result :
Disclaimer: I just wanted to play with the idea your question gave me, and share it with you. Don't apply this kind of code just because you can. Indeed, this code should be refined to cover all cases efficiently before even being used, and even then, a simple
typedef std::string S_ ;
would be better IMHO.AFAIK, I wouldn't use it because I'm happy with the current STL API.
And what about C++0x ?
In C++0x, you'll be able to create your own literals. The code will be kinda like :
And you'll use it so :
For more information, see the following SO question: What new capabilities do user-defined literals add to C++?.