Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c++; 你好,我在C++中编写了一个字符串类,我想在运算符+(const char *)中重用操作符+(const string和String 1)和重用运算符+和操作符=字符串操作+=(const string & String 1)和操作符+=(const char *)。我的代码如下。谢谢大家:) String&String::operator=(常量String&string1){ 使用std::nothrow; 使用std::endl; 使用std::cout; 破坏(); size=string1.getSize(); //如果(*此!=string1){ 如果(getSize()!=0) { 数据=新的(nothrow)字符[getSize()]; 如果(数据==NULL) cout_C++_Oop - Fatal编程技术网

如何在c++; 你好,我在C++中编写了一个字符串类,我想在运算符+(const char *)中重用操作符+(const string和String 1)和重用运算符+和操作符=字符串操作+=(const string & String 1)和操作符+=(const char *)。我的代码如下。谢谢大家:) String&String::operator=(常量String&string1){ 使用std::nothrow; 使用std::endl; 使用std::cout; 破坏(); size=string1.getSize(); //如果(*此!=string1){ 如果(getSize()!=0) { 数据=新的(nothrow)字符[getSize()]; 如果(数据==NULL) cout

如何在c++; 你好,我在C++中编写了一个字符串类,我想在运算符+(const char *)中重用操作符+(const string和String 1)和重用运算符+和操作符=字符串操作+=(const string & String 1)和操作符+=(const char *)。我的代码如下。谢谢大家:) String&String::operator=(常量String&string1){ 使用std::nothrow; 使用std::endl; 使用std::cout; 破坏(); size=string1.getSize(); //如果(*此!=string1){ 如果(getSize()!=0) { 数据=新的(nothrow)字符[getSize()]; 如果(数据==NULL) cout,c++,oop,C++,Oop,只要您的字符串类型有一个采用const char*的构造函数,您就可以免费获得这些。编译器将在需要字符串类型的任何地方使用该构造函数将C样式字符串转换为您的字符串类型。例如 String346 str("abcd"); String346 res = str + "efgh"; 这将使用成员函数operator+(const String346&)在res中存储“abcdefgh” 然而,这对于operator+来说不是一个好的接口,原因有几个。相反,正如一些评论所建议的,将operator+

只要您的字符串类型有一个采用
const char*
的构造函数,您就可以免费获得这些。编译器将在需要字符串类型的任何地方使用该构造函数将C样式字符串转换为您的字符串类型。例如

String346 str("abcd");
String346 res = str + "efgh";
这将使用成员函数
operator+(const String346&)
res
中存储
“abcdefgh”

然而,这对于
operator+
来说不是一个好的接口,原因有几个。相反,正如一些评论所建议的,将
operator+=
作为成员函数,将
operator+(const-String346&,const-String346&)定义为自由函数,并使用
operator+=
实现它


如果你这样做,转换仍然有效。

一般是,<代码> +/>代码使用<代码> += 。你的赋值操作符在自我分配上中断。C++需要很多东西。但是如果它们是另一个字符串类,则没有。可以通过引用返回本地变量吗?就像这个代码重复使用“TEMP”一样。谢谢你的帮助。我也很感激我能同时成为operator+和operator+=会员functions@isaac_34--好的,忽略我回答的最后两段。
String346 str("abcd");
String346 res = str + "efgh";