#include<iostream>
#include<stdlib.h>
#include<cstring>
using namespace std;
class Cstring
{
public:
Cstring(char *);
static int get_total(Cstring &);//静态成员函数可以在外面定义,要引用非静态成员,只能通过传参的方式
void show();
Cstring(Cstring &);
private:
char *strings;
static int total_len;
int len;
};
Cstring::Cstring(Cstring &t)
{
int length=strlen(t.strings)+1;
strings=new char[length];
if(strings==0)
{
cout<<"申请内存失败"<<endl;
exit(1);
}
strcpy(strings,t.strings);
len=t.len;
}
Cstring::Cstring(char *p)
{
int length=strlen(p)+1;
strings =new char[length];
if(strings==0)
{
cout<<"申请内存失败"<<endl;
exit(1);
}
strcpy(strings,p);
}
int Cstring::total_len=0;
int Cstring::get_total(Cstring &str)
{
total_len+=strlen(str.strings);
cout<<str.strings<<endl;
return total_len;
}
int main()
{
Cstring string1("dwz");
Cstring string2("dxfaa");
cout<<Cstring::get_total(string2)<<endl;
cout<<Cstring::get_total(string1)<<endl;
return 0;
}
静态成员函数
最新推荐文章于 2024-06-16 00:05:14 发布