c++ - overloaded subscript operator - []

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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值