CString a = "你好"; + “世界!”;是否可以?

发布于 2024-09-01 13:27:09 字数 341 浏览 2 评论 0 原文

我正在制作自己的字符串 class 并且我想确保 CString a = "Hello " + "World!"; 有效(即不给编译器错误例如:无法添加 2 个指针)。

我的字符串 class 在需要时自动转换为 char*,因此编写 printf(a) 不会破坏代码。

有什么方法可以替换 字符 周围的编译器行为吗? (即在引号之间,"abc")。或者,更改 + 运算符的行为来处理字符串

I'm making my own string class and I'd like to ensure that CString a = "Hello " + "World!"; works (i.e. does not give a compiler error such as: cannot add 2 pointers).

My string class automatically converts to char* when needed and thus writing printf(a) would not break the code.

Is there any way to replace the compiler behavior around characters ? (i.e. between inverted commas, "abc"). Or, alternatively, to change the behavior of the + operator to handle strings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静赏你的温柔 2024-09-08 13:27:09

您不能完全这样做,因为您只能重载类类型的运算符,而字符串文字没有类类型。

您可以利用预处理期间发生的字符串文字串联:

CString a = "Hello " "World!";

或者您可以创建一个 CString 临时对象并附加到它:

CString a = CString("Hello ") + "World!";

或者您可以有一个 CString 构造函数需要多个参数:

CString a("Hello ", "World!");

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:

CString a = "Hello " "World!";

or you can create a CString temporary and append to it:

CString a = CString("Hello ") + "World!";

or you could have a CString constructor that takes more than one argument:

CString a("Hello ", "World!");
自由如风 2024-09-08 13:27:09

正确答案

是No。
您不能重载内置运算符,例如指针。

James McNellis 已经回答了这个问题,所以我不再详细说明。

一个可能的替代方案...

(我使用 std::string 因为我没有关于你的内部字符串的信息)

使用 typedef 会给你的语法添加一些糖:

typedef std::string S_ ;

int main(int argc, char* argv[])
{
   std::string s = S_("Hello") + S_(" World") ;

   std::cout << "s : " << s << std::endl ;

   return 0 ;
}

但是,我不会用两个字符的符号污染全局命名空间只是为了一点点糖...据我所知,代码效率很低(创建了两个字符串对象,加上一个临时对象,不能保证编译器会将其全部优化掉.. .)

出于好奇......出于

好奇,通过将字符串包装成一个薄类,您可以“添加”这两个指针。

首先,让我们创建包装器:

class StringThinWrapper
{
   public :

      StringThinWrapper(const char * p) : m_p(p) {}
      operator const char * () const { return m_p ; }

   private :
      const char * const m_p ;
} ;

如您所见,它都是内联的,并且不会执行任何操作...不过,它能够将自身转换为 const char * 指针(这种 hack 很危险,所以请确保它是您想要的)想做)。

然后,对于这个包装器,让我们重载加法运算符:

inline std::string operator + (const StringThinWrapper & lhs, const StringThinWrapper & rhs)
{
   std::string s(lhs) ;
   s += rhs ;
   return s ;
}

现在,让我们使用包装器编写一个 main 函数,为便于使用而进行了类型定义:

typedef StringThinWrapper S_ ;

int main(int argc, char* argv[])
{
   std::string s = S_("Hello") + S_(" World") ;

   std::cout << "s : " << s << std::endl ;

   return 0 ;
}

它编译并给出以下结果:

s:你好世界

免责声明:我只是想尝试一下你的问题给我的想法,并与你分享。不要仅仅因为可以就应用此类代码。事实上,在使用这段代码之前就应该对其进行改进,以有效地覆盖所有情况,即使如此,一个简单的 typedef std::string S_ ; 恕我直言,会更好。

AFAIK,我不会使用它,因为我对当前的 STL API 很满意。

那么 C++0x 呢?

在 C++0x 中,您将能够创建自己的文字。代码有点像:

std::string operator "str"(const char * p)
{ 
    return std::string(p); 
}

您将这样使用它:

int main(int argc, char * argv[])
{
   std::string s = "Hello"str + " World"str ;

   std::cout << "s : " << s << std::endl ;

   return 0 ;
}

有关更多信息,请参阅以下问题:用户定义的文字为 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:

typedef std::string S_ ;

int main(int argc, char* argv[])
{
   std::string s = S_("Hello") + S_(" World") ;

   std::cout << "s : " << s << std::endl ;

   return 0 ;
}

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 :

class StringThinWrapper
{
   public :

      StringThinWrapper(const char * p) : m_p(p) {}
      operator const char * () const { return m_p ; }

   private :
      const char * const m_p ;
} ;

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 :

inline std::string operator + (const StringThinWrapper & lhs, const StringThinWrapper & rhs)
{
   std::string s(lhs) ;
   s += rhs ;
   return s ;
}

And now, let's write a main function using the wrapper, typedefed for ease of use :

typedef StringThinWrapper S_ ;

int main(int argc, char* argv[])
{
   std::string s = S_("Hello") + S_(" World") ;

   std::cout << "s : " << s << std::endl ;

   return 0 ;
}

Which compiles and gives the following result :

s : Hello World

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 :

std::string operator "str"(const char * p)
{ 
    return std::string(p); 
}

And you'll use it so :

int main(int argc, char * argv[])
{
   std::string s = "Hello"str + " World"str ;

   std::cout << "s : " << s << std::endl ;

   return 0 ;
}

For more information, see the following SO question: What new capabilities do user-defined literals add to C++?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文