You can overload the subscript operator, a typical scenario that you may use the subscriptor overload operator is when you design the String class, where you want the user to access the element at ith indext, which is a char type.
However, what is the return type of the subscriptor operator ? for const object, it should return a const char reference or we simple return a char reference regardless?
here is the example, below is the definition of MyString class.
/**
* file
* MyString.h
*
* this header is the demonstrate the use of user defined constructor and overload operator
*/
#include <iostream>
class MyString
{
public :
// overload set of constructors
// provide automatic initialization
MyString(const char * = 0);
MyString (const MyString & );
// destructor: automatic deinitialization
~MyString();
// overload set of assignment operators
MyString& operator ==(const char *) const;
MyString& operator ==(const MyString &) const;
// member access function
int size() { return _size; }
char * c_str() { return _string; }
const char& operator[](int elem) const;
char & operator[] (int elem);
//// I love better the definition above
//// ths is not safe..
//char& operator[](int elem) const;
private:
int _size;
char * _string;
protected:
};
As you can see that we providing two overloaded [] operator functions.
below is the MyString implementation code.
/**
* file
* MyString.cpp
*
* this is the MyString.cpp
*/
#include "stdafx.h"
#include "MyString.h"
#include <iostream>
#include <vector>
#include <assert.h>
#include <cstddef>
#include <utility>
#include <cassert>
#include <cstring>
using std::cout;
using std::endl;
using std::cerr;
using std::strcat;
using std::strlen;
using std::strcmp;
using std::strcpy;
/**
MyString
*
*/
MyString::MyString(const char * name_) {
if (name_ != NULL) {
_size = strlen(name_);
_string = new char[_size + 1];
strcpy(_string, name_);
} else
{
_string = NULL;
_size = 0;
}
}
MyString::MyString(const MyString& rhs) {
// aovid the self coyping
if (this != &rhs) {
_size = rhs._size;
if (rhs._string != NULL) {
delete _string;
_string = new char[_size + 1];
strcpy(_string, rhs._string);
}
}
}
MyString::~MyString() {
delete _string;
}
char & MyString::operator[](int elem) {
assert(elem >= 0 && elem < _size);
return _string[_size];
}
const char & MyString::operator[](int elem) const {
assert(elem >= 0 && elem < _size);
return _string[_size];
}
so that given the above declaration , you can use the [] operator as follow.
void test_MyString()
{
MyString mystring;
char ch = mystring[0];
mystring[0] = 'b';
const MyString & mystringRef = mystring;
mystringRef[0] = 'a'; // error, since the conference MyString& return a const char referece, it does not allow writting..
}
the code above shows my way of designing overload subscriptor operator.
however, to simplify the task, you can also design the subscriptor as follow.
suppose that you can change the declaration to this :
class MyString {
char & operator[](int elem) const {
assert(elem >= 0 && elem < _size);
return _string[_size];
}
}
however, the code
void test_MyString()
{
MyString mystring;
char ch = mystring[0];
mystring[0] = 'b';
const MyString & mystringRef = mystring;
mystringRef[0] = 'a'; // it works, but however,this is not the desired behavior, as you directly can modify the const object
}
C++字符串类下标运算符重载
本文探讨了C++中自定义字符串类MyString的下标运算符[]重载实现。针对不同情况(常量与非常量对象),讨论了返回引用类型的选择,并通过示例展示了如何正确使用这些运算符。
1076

被折叠的 条评论
为什么被折叠?



