#include<iostream>
#include<cstdlib>
using namespace std;
class CharPair{
public:
CharPair(){
}
CharPair(char first, char second): first(first), second(second){
}
char &operator[](int index);
private:
char first;
char second;
};
int main(){
CharPair a;
a[1] = 'A';
a[2] = 'B';
cout<<a[1]<<" "<<a[2]<<endl;
cout<<"Enter two chars:"<<endl;
cin>>a[1]>>a[2];
cout<<"New latters;"<<endl;
cout<<a[1]<<" "<<a[2]<<endl;
}
char &CharPair::operator[](int index){
if(index == 1){
return first;
}else if(index == 2){
return second;
}else{
cout<<"Error index!\n";
exit(1);
}
}
C++重载[]数组操作符
最新推荐文章于 2022-04-26 15:25:55 发布