c++ 问题 十万火急

来源:百度知道 编辑:UC知道 时间:2024/06/08 04:32:57
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
class CStrtemp
{
char* str; //私有数据成员
public:
CStrtemp(char* s)
{
str=new char[strlen(s)+1]; //为str分配存储空间
if(!str)
{
cerr<<"Allocationg Error"<<endl;//cerr是标准流对象,参看第7章
exit(-1);
}
strcpy(str,s); //将字符串复制到str指向的存储空间中
}
CStrtemp(const CStrtemp&); //复制构造函数的复制,省略形参
~CStrtemp() //析构函数
{
if(str!=NULL)delete[] str; //释放str指向的存储空间
}
void show() //成员函数
{
cout<<str<<endl;
}
void set(char* s); //赋值新串
};
CStrtemp::CStrtemp(const CStrtemp& temp) //复制构造函数的实现
{
str=new char[strlen(temp.str)+1];
if(!str)
{
cerr<<"error in apply new space."<<endl;

CStrtemp B; 只是声明

CStrtemp Input(CStrtemp temp){
} 是函数声明和定义函数。CStrtemp 后面没有两个冒号,所以它不是 CStrtemp class 的 成员或成员函数。
而是 普通的 独立的 函数,它传入 CStrtemp temp,用
temp.set(s),修改字符串后,通过return
送返 新的 CStrtemp 对象。

Input(A) 在表达式里,是调用,输入 CStrtemp型 对象,送回 新的 CStrtemp型 对象。

通过 赋值,把新对象 给了B。

CStrtemp B=Input(A); 这句,既是声明B,又初始化了B。

都什么跟什么?

创建对象B的时候已经传了参数,只不过他传入的是一个对象A而已.