CString a =“Hello” +“World!”;是否可以? [英] CString a = "Hello " + "World!"; Is it possible?

查看:130
本文介绍了CString a =“Hello” +“World!”;是否可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建我自己的字符串 class ,我想确保 CString a =Hello+World! 工程(即不会给出编译器错误,例如:无法添加2个指针)。



当需要时,我的字符串 class 自动转换为 char * ,从而写入 printf a)不会破坏代码。



有没有办法替换字符 (即在反向逗号之间,abc)。或者,改变 + 运算符的行为以处理字符串

解决方案

正确答案



否。
不能重载内建函数的运算符,例如指针。



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



一个可能的替代...



(我使用std :: string因为我没有信息-house 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;
}

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



为了好奇心...



好奇的是,通过将字符串包装到一个thin类中,你可以添加这两个指针。



首先,创建包装器:

  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 函数使用包装器,typedefed为易于使用:

  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:Hello World


免责声明:我只想玩想法你的问题给了我,并与你分享。不要应用这种代码只是因为你可以。实际上,这个代码应该被细化,以便甚至在被使用之前有效地覆盖所有情况,即使这样,一个简单的 typedef std :: string S_; p>

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



C ++ 0x?



在C ++ 0x中,您可以创建自己的文字。代码将类似于:

  std :: string运算符str(const char * p)
{
return std :: string(p);
}

您将使用它:

  int main(int argc,char * argv [])
{
std :: string s =Hellostr + str

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

return 0;
}

有关详细信息,请参阅以下SO问题: http://stackoverflow.com/questions/237804


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?

解决方案

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: http://stackoverflow.com/questions/237804.

这篇关于CString a =“Hello” +“World!”;是否可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆