[]运算符:
#ifndef _STRING_H_
#define _STRING_H_
class String
{
public: void Display();
String(char *str);
char& operator[](unsigned int index);
private:
char *str_;
};
#endif
#include<iostream>
#include"String.h"
using namespace std;
void String::Display()
{
cout<< str_ <<endl;
}
String::String(char *str)
{
int len = strlen(str)+1;
str_ = new char[len];
strcpy(str_,str);
}
char& String::operator[](unsigned int index)
{
return str_[index];;
}
#include"String.h"
using namespace std;
#include"String.h"
using namespace std;
int main()
{
String s1("hello");
s1[2] = 'E';
s1.Display();
}
结果:
还有一种加上const
原型是:const char& operator[](unsigned int index)const;
#ifndef _STRING_H_
#define _STRING_H_
class String
{
public: void Display();
String(char *str);
const char& operator[](unsigned int index)const;
private:
char *str_;
};
#endif
#include<iostream>
#include"String.h"
using namespace std;
void String::Display()
{
cout<< str_ <<endl;
}
String::String(char *str)
{
int len = strlen(str)+1;
str_ = new char[len];
strcpy(str_,str);
}
const char& String::operator[](unsigned int index)const
{
return str_[index];
int main()
{
String s1("hello");
s1[2] = 'E';
s1.Display();
}
结果:
+运算符重载只能用友元函数
#ifndef _STRING_H_
#define _STRING_H_
class String
{
public:
String(char *str);
void Display();
friend String operator+(const String& s1,const String& s2);
private:
char *str_;
};
#endif
#include<iostream>
#include"String.h"
using namespace std;
int main()
{
String s1("hello");
String s2("world");
String s3 = s1 + s2;
s3.Display();
}
结果:
+=符号的重载,用成员函数
#ifndef _STRING_H_
#define _STRING_H_
class String
{
public:
String(char *str);
void Display();
String& operator+=(const String& s);
private:
char *str_;
};
#endif
#include<iostream>
#include"String.h"
#include<string.h>
using namespace std;
void String::Display()
{
cout<<str_<<endl;
}
String ::String(char *str)
{
int len = strlen(str)+1;
str_ = new char[len];
memset(str_,0,len);
strcpy(str_,str);
}
String& String::operator+=(const String& s)
{
int len = strlen(str_)+strlen(s.str_)+1;
char *newptr = new char[len];
memset(newptr,0,len);
strcpy(newptr,str_);
strcat(newptr,s.str_);
delete []str_;
str_ = new char [len];
strcpy(str_,newptr);
return *this;
}
#include<iostream>
#include"String.h"
int main()
{
String s1("hello");
String s2("world");
s2 += s1;
s2.Display();
}
结果:
流运算重载(友元函数)
输入istream与输出ostream
#ifndef _STRING_H_
#define _STRING_H_
#include<iostream>
using namespace std;
class String
{
public:
String();
String(char *str);
void Display();
friend istream& operator>>(istream &in, String& s);
friend ostream& operator<<(ostream &out,const String& s);
private:
char *str_;
};
#endif
#include<iostream>
#include"String.h"
#include<string.h>
using namespace std;
String::String()
{
}
void String::Display()
{
cout<<str_<<endl;
}
String ::String(char *str)
{
int len = strlen(str)+1;
str_ = new char[len];
memset(str_,0,len);
strcpy(str_,str);
}
istream& operator>>(istream &in, String& s)
{
char buffer[2048];
in>>buffer;
int len = strlen(buffer)+1;
delete []s.str_;
s.str_ = new char [len];
strcpy(s.str_,buffer);
return in;
}
ostream& operator<<(ostream &out,const String& s)
{
out<<s.str_;
return out;
}
int main()
{
String s("hello world");
String s1;
cout<<s<<endl;
cin>>s1;
cout<<s1<<endl;
}
类类型转换运算符
Operator int();
Char* ptr=static—cast<char*s>
#ifndef _STRING_H_
#define _STRING_H_
class String
{
public:
void Display();
String& operator=(const String& other);
String(char *str);
operator char*();
private:
char *str_;
};
#endif
#include<iostream>
#include"String.h"
using namespace std;
String::operator char*()
{
return str_;
}
void String::Display()
{
cout<<str_<<endl;
}
String::String(char *str)
{
int len = strlen(str)+1;
str_ = new char[len];
memset(str_,0,len);
strcpy(str_,str);
}
String& String::operator=(const String& other)
{
if( this == &other)
{
return *this;
}
else
{
int len = strlen(other.str_)+1;
str_ = new char [len];
memset(str_,0,len);
strcpy(str_,other.str_);
}
}
#include<iostream>
#include"String.h"
using namespace std;
int main()
{
String s("hello");
String s1(s);
s1.Display();
char *ptr = static_cast<char*>(s1);
cout<< ptr <<endl;
return 0;
}
结果:
New运算符
1、operator new:只分配所要求的空间,不调用相关对象的构造函数。
可以被重载,重载时,返回类型必须声明为void*, 重载时,第一个参数类型必须为表达要求分配空间的大小(字节),类型为size_t,重载时,可以带其它参数
2、new operator:
调用operator new分配足够的空间,并调用相关对象的构造函数, 不可以被重载
3.placement new:不分配空间,只从已经存在的空间上分配内存,调用构造函数。