【c++】运算符重载(下)

[]运算符:

#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=staticcast<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,重载时,可以带其它参数

2new operator

调用operator new分配足够的空间,并调用相关对象的构造函数, 不可以被重载

3.placement new:不分配空间,只从已经存在的空间上分配内存,调用构造函数。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值