为什么重载的+运算符函数中的临时对象不能被析构?
来源:互联网 发布:在国外怎么用淘宝 编辑:程序博客网 时间:2024/06/11 17:00
如题……
//String 类声明及定义 String.h
#include<iostream>
using std::cout;
using std::endl;
class String
{
public:
String();
String(const String &t);
String(const char *ch);
String & operator=(const String &t);
String operator+(const String &t);
void display();
virtual ~String();
private:
char *data;
int SIZE;
};
String::String()
{
data=0;
SIZE=0;
cout<<"无参数构造函数调用..."<<endl;
}
String::~String()
{
cout<<SIZE<<" 析构函数调用中。。。"<<endl;
delete []data;
}
String::String(const String &t)
{
SIZE=t.SIZE;
data=new char[SIZE+1];
for(int i=0;i<SIZE;i++)
data[i]=t.data[i];
data[i]='/0';
cout<<SIZE<<" 复制构造函数调用中。。。"<<endl;
}
String::String(const char *ch)
{
for(int i=0;ch[i]!='/0';i++);//求字符串的个数。
SIZE=i;
data=new char[SIZE+1];
for(i=0;i<SIZE;i++)
data[i]=ch[i];
data[i]='/0';
cout<<SIZE<<" 带指向字符串指针作参数的构造函数。。。"<<endl;
}
String & String::operator =(const String &t)
{
delete []data;
SIZE=t.SIZE;
data=new char[SIZE+1];
for(int i=0;i<SIZE;i++)
data[i]=t.data[i];
data[i]='/0';
cout<<SIZE<<" 赋值运算符执行中..."<<endl;
return *this;
}
String String::operator +(const String &t)
{
String temp;
temp.SIZE=SIZE+t.SIZE;
temp.data=new char[SIZE+1];
for(int i=0;i<temp.SIZE;i++)
if(i<SIZE)
temp.data[i]=data[i];
else
temp.data[i]=t.data[i-SIZE];
temp.data[i]='/0';
return temp;//为什么temp的无法析构?
}
void String::display()
{
for(int i=0;i<SIZE;i++)
cout<<data[i];
cout<<endl;
}
//主函数main.cpp
#include"String.h"
int main()
{
String str0 = "man";
str0.display();
String str1="Welcome";
str1.display();
str0=str1+"China";
str0.display();
return 0;
}
- 为什么重载的+运算符函数中的临时对象不能被析构?
- 为什么C++赋值运算符重载函数不能被继承?
- 为什么C++赋值运算符重载函数不能被继承?
- 为什么C++赋值运算符重载函数不能被继承?
- 为什么C++赋值运算符重载函数不能被继承?
- 为什么C++赋值运算符重载函数不能被继承?
- 为什么C++赋值运算符重载函数不能被继承?
- 为什么C++赋值运算符重载函数不能被继承?
- 【面向对象程序设计常见面试题】流运算符为什么不能通过类的成员函数重载?一般怎么解决?(7)
- 流运算符为什么不能重载为成员函数,只能用友元函数重载
- 流运算符为什么不能重载为成员函数,只能用友元函数重载
- 不能重载的运算符
- 不能重载的运算符
- 为什么C++赋值运算符重载函数不能被继承? 【转】
- 详谈面向对象的重载——>“函数重载”与“运算符重载”
- 函数中的临时对象
- 函数重载,覆盖,参数缺省值,不能被重载的运算符,元芳,你怎么看?
- c++中为什么有不能重载的运算符(摘录)
- 新人报道 ~
- vs2010
- 深度覆盖
- 打个
- 快乐的种子
- 为什么重载的+运算符函数中的临时对象不能被析构?
- 在myEclipse中清除所有断点
- 注册成功
- 兄弟连Linux在线课堂:第7讲 Linux用户管理
- 2010年12月21日
- 真强大
- 朋友
- C#語法
- 使用C#编写BHO实现残障人群上网冲浪无障碍