4.提高
1.運算符重載機制 編譯器實現運算符重載實際上就是通過函數重載實現的,可分為全局函數方式,也可分為成員函數方式進行重載,并沒有改變原操作符的屬性和語義。只是針對某個特定類定義一種新的數據類型操作。 2.重載賦值運算符
賦值運算符重載用于對象數據的復制 operator= 必須重載為成員函數 重載函數原型為: 類型 & 類名 :: operator= ( const 類名 & ) ;
結論:
1 先釋放舊的內存
2 返回一個引用
3 =操作符 從右向左
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std ;
class Name
{
public :Name(
const char *myp){m_len =
strlen (myp);m_p =(
char *)
malloc (m_len +
1 );
strcpy (m_p, myp);}Name(
const Name& obj1){m_len = obj1.m_len;m_p = (
char *)
malloc (m_len +
1 );
strcpy (m_p, obj1.m_p);}Name&
operator =(Name &obj1){
if (
this ->m_p != NULL){
delete [] m_p;m_len =
0 ;}
this ->m_len = obj1.m_len;
this ->m_p =
new char [m_len+
1 ];
strcpy (m_p, obj1.m_p);
return *
this ;}~Name(){
if (m_p != NULL){
free (m_p);m_p = NULL;m_len =
0 ;}}
protected :
private :
char *m_p ;
int m_len;
};
void objplaymain()
{Name obj1(
"abcdefg" );Name obj2 = obj1; Name obj3(
"obj3" );obj3 = obj1; obj1 = obj2 = obj3;
}
void main()
{objplaymain();
cout <<
"hello..." <<endl;system(
"pause" );
return ;
}
3.重載下標運算符
[ ]運算符用于訪問數據對象的元素 重載格式 類型 類 :: operator[] ( 類型 ) ; 只能用成員函數重載,不能用友元函數重載
示例: 設 x 是類 X 的一個對象,則表達式 x [ y ] 可被解釋為 x . operator [ ] ( y )
函數返回值當左值需要返回一個引用!
4.帶下標和相等操作符的數組類
#ifndef NEWARRAY_H
#define NEWARRAY_H
#include <iostream>
#include <stdlib.h> class NewArray
{
public :
NewArray ();NewArray(
int _len);NewArray(
const NewArray & obj);~NewArray();
void setData(
int index,
int var );
int getData(
int index);
int length();
int &
operator [](
int i);NewArray&
operator =(NewArray& obj);
bool operator ==(NewArray& obj);
bool operator !=(NewArray& obj);
private :
int m_len;
int *m_buf;
};
#endif // NEWARRAY_H
#include "newarray.h" NewArray::NewArray()
{m_buf = NULL;m_len = -
1 ;
}NewArray::NewArray(
int _len)
{
if (_len <
0 )_len =
0 ;m_len = _len;m_buf =
new int [m_len];}
NewArray::NewArray(
const NewArray & obj)
{m_len = obj.m_len;m_buf =
new int [m_len];
for (
int i =
0 ;i < m_len;i++){m_buf[i] = obj.m_buf[i];}
}
NewArray::~NewArray()
{
if (m_buf != NULL){delete []m_buf;m_buf = NULL;m_len = -
1 ;}
}
void NewArray::setData(
int index,
int var )
{m_buf[index] =
var ;
}
int NewArray::getData(
int index)
{
return m_buf[index];
}
int NewArray::length()
{
return m_len;
}
int & NewArray::
operator [](
int i)
{
return m_buf[i];
}
NewArray& NewArray::
operator =(NewArray& obj)
{
if (m_buf != NULL){delete []m_buf;m_len = -
1 ;m_buf = NULL;}m_len = obj.m_len;m_buf =
new int [m_len];
for (
int i =
0 ;i < m_len;i++){m_buf[i] = obj.m_buf[i];}
return *
this ;}
bool NewArray::
operator ==(NewArray& obj)
{
if (m_len != obj.m_len){
return false ;}
for (
int i =
0 ;i < m_len;i++){
if (m_buf[i] != obj.m_buf[i]){
return false ;}}
return true ;
}
bool NewArray::
operator !=(NewArray& obj)
{
return !((*
this ) == obj);
}
#include "newarray.h"
using namespace std ;
int main()
{NewArray a1(
10 );
for (
int i=
0 ; i<a1.length(); i++){a1.setData(i, i);a1[i] = i;}
cout <<
"\na1: " ;
for (
int i=
0 ; i<a1.length(); i++){
cout <<a1[i]<<
"\t" ;}
cout <<endl;NewArray a2 = a1;
cout <<
"\na2: " ;
for (
int i=
0 ; i<a2.length(); i++){
cout <<a2.getData(i)<<
" " ;}
cout <<endl;NewArray a3(
5 );{a3 = a1;a3 = a2 = a1;
cout <<
"\na3: " ;
for (
int i=
0 ; i<a3.length(); i++){
cout <<a3[i]<<
" " ;}}
if (a3 == a1){
printf (
"\nequal\n" );}
else {
printf (
"\nnot equal\n" );}
if (a3 != a1){
printf (
"\nnot equal\n" );}
else {
printf (
"\nequal\n" );}
cout <<
"hello..." <<endl;
return 1 ;
}
5.重載函數調用運算符
類型 類 ::
operator () ( 表達式表 ) ;
例1 設 x 是類 X 的一個對象,則表達式 x ( arg1, arg2, … ) 可被解釋為 x . operator () (arg1, arg2, … ) 案例:
#include <iostream>
class F{
public :
double operator ( ) (
double x ,
double y ) ;} ;
double F ::
operator ( ) (
double x ,
double y ){
return x * x + y * y ; }
void main ( )
{
F f ;
f.getA();
cout << f (
5.2 ,
2.5 ) << endl ;
}
#include <iostream.h>
class F { public :
double memFun (
double x ,
double y ) ;} ;
double F :: memFun (
double x ,
double y ){
return x * x + y * y ; }
void main ( )
{
F f ;cout << f.memFun (
5.2 ,
2.5 ) << endl ;
}
6.不建議重載的運算符
理論知識: 1)&&和||是C++中非常特殊的操作符 2)&&和||內置實現了短路規則 3)操作符重載是靠函數重載來完成的 4)操作數作為函數參數傳遞 5)C++的函數參數都會被求值,無法實現短路規則
#include <cstdlib>
#include <iostream> using namespace std ;
class Test
{
int i;
public :Test(
int i){
this ->i = i;}Test
operator + (
const Test& obj){Test ret(
0 );
cout <<
"執行+號重載函數" <<endl;ret.i = i + obj.i;
return ret;}
bool operator && (
const Test& obj){
cout <<
"執行&&重載函數" <<endl;
return i && obj.i;}
};
void main()
{
int a1 =
0 ;
int a2 =
1 ;
cout <<
"注意:&&操作符的結合順序是從左向右" <<endl;
if ( a1 && (a1 + a2) ){
cout <<
"有一個是假,則不在執行下一個表達式的計算" <<endl;}Test t1 =
0 ;Test t2 =
1 ;
if ( (t1 + t2) && t1){
cout <<
"兩個函數都被執行了,而且是先執行了+" <<endl;}{
cout <<
"兩個函數都被執行了,而且是先執行了+" <<endl;}system(
"pause" );
return ;
}
5.字符串類的實現
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
using namespace std ;
#include <stdlib.h>
#include <string.h>
class MyString
{
public :MyString();MyString(
int _len);MyString(
const char *_str);MyString(
const MyString & obj);~MyString();MyString&
operator =(
const MyString & obj);MyString&
operator =(
const char * _str);
bool operator ==(
const MyString & obj);
bool operator ==(
const char * _str);
bool operator !=(
const MyString & obj);
bool operator !=(
const char * _str);
bool operator >(
const MyString & obj);
bool operator >(
const char * _str);
bool operator <(
const MyString & obj);
bool operator <(
const char * _str);
char &
operator [](
int index);
friend ostream&
operator <<(ostream & out,MyString & obj);
friend istream&
operator >>(istream & in,MyString & obj);
private :
int m_len;
char *m_str;
};
#endif
#include "mystring.h" MyString::MyString()
{m_len =
0 ;m_str = NULL;
}
MyString::MyString(
int _len)
{
if (_len <
0 )_len =
0 ;m_len = _len;m_str =
new char [m_len+
1 ];
memset (m_str,
0 ,m_len);
}MyString::MyString(
const char *_str)
{
if (_str == NULL){m_len =
0 ;m_str =
new char [m_len+
1 ];
strcpy (m_str,
"" );}
else {m_len =
strlen (_str);m_str =
new char [m_len+
1 ];
strcpy (m_str,_str);}}
MyString::MyString(
const MyString & obj)
{m_len = obj.m_len;m_str =
new char [m_len+
1 ];
strcpy (m_str,obj.m_str);
}
MyString::~MyString()
{
if (m_str != NULL){
delete []m_str;m_str = NULL;m_len =
0 ;}
}MyString& MyString::
operator =(
const MyString & obj)
{
if (m_str != NULL){
delete []m_str;m_str = NULL;m_len =
0 ;}m_len = obj.m_len;m_str =
new char [m_len+
1 ];
strcpy (m_str,obj.m_str);
return *
this ;
}MyString& MyString::
operator =(
const char * _str)
{
if (m_str != NULL){
delete []m_str;m_str = NULL;m_len =
0 ;}
if (_str == NULL){m_len =
0 ;m_str =
new char [m_len+
1 ];
strcpy (m_str,
"" );}
else {m_len =
strlen (_str);m_str =
new char [m_len+
1 ];
strcpy (m_str,_str);}
return *
this ;
}
bool MyString::
operator ==(
const MyString & obj)
{
if (m_len != obj.m_len){
return false ;}
return !
strcmp (m_str,obj.m_str);}
bool MyString::
operator ==(
const char * _str)
{
if (_str == NULL){
if (m_len ==
0 ){
return true ;}
else {
return false ;}}
else {
if (m_len ==
strlen (_str)){
return !
strcmp (m_str,_str);}
else {
return false ;}}}
bool MyString::
operator !=(
const MyString & obj)
{
return !((*
this ) == obj);
}
bool MyString::
operator !=(
const char * _str)
{
return !((*
this ) == _str);
}
bool MyString::
operator >(
const MyString & obj)
{
if (
strcmp (m_str,obj.m_str) >
0 ){
return true ;}
else {
return false ;}
}
bool MyString::
operator >(
const char * _str)
{
if (
strcmp (m_str,_str) >
0 ){
return true ;}
else {
return false ;}
}
bool MyString::
operator <(
const MyString & obj)
{
if (
strcmp (m_str,obj.m_str) <
0 ){
return true ;}
else {
return false ;}
}
bool MyString::
operator <(
const char * _str)
{
if (
strcmp (m_str,_str) <
0 ){
return true ;}
else {
return false ;}
}
char & MyString::
operator [](
int index)
{
return m_str[index];
}ostream&
operator <<(ostream & out,MyString & obj)
{out<<obj.m_str;
return out;
}istream&
operator >>(istream & in,MyString & obj)
{in>>obj.m_str;
return in;
}
#define _CRT_SECURE_NO_WARNINGS #include "mystring.h" void main01()
{MyString s1;MyString s2(
"s2" );MyString s2_2 = NULL;MyString s3 = s2;MyString s4 =
"s4444444444" ;s4 = s2;s4 =
"s2222" ;s4[
1 ] =
'4' ;
printf (
"%c" , s4[
1 ]);
cout <<s4 <<endl;
cout <<
"hello..." <<endl;system(
"pause" );
return ;
}
void main02()
{MyString s1;MyString s2(
"s2" );MyString s3 = s2;
if (s2 ==
"aa" ){
printf (
"相等" );}
else {
printf (
"不相等" );}
if (s3 == s2){
printf (
"相等" );}
else {
printf (
"不相等" );}}
void main03()
{MyString s1;MyString s2(
"s2" );MyString s3 = s2;s3 =
"aaa" ;
if (s3 <
"bbbb" ){
printf (
"s3 小于 bbbb" );}
else {
printf (
"s3 大于 bbbb" );}MyString s4 =
"aaaaffff" ;
cout <<s4<<endl;
}
void main011()
{MyString s1(
128 );
cout <<
"\n請輸入字符串(回車結束)" ;
cin >>s1;
cout <<s1;system(
"pause" );}
int main()
{MyString s1(
128 );
cout <<
"\n請輸入字符串(回車結束)" ;
cin >>s1;
cout <<s1<<endl;system(
"pause" );
return 0 ;}
總結
操作符重載是C++的強大特性之一 操作符重載的本質是通過函數擴展操作符的語義 operator關鍵字是操作符重載的關鍵 friend關鍵字可以對函數或類開發訪問權限 操作符重載遵循函數重載的規則 操作符重載可以直接使用類的成員函數實現 =, [], ()和->操作符只能通過成員函數進行重載 ++操作符通過一個int參數進行前置與后置的重載 C++中不要重載&&和||操作符
總結
以上是生活随笔 為你收集整理的C++之运算符重载(下) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。