重载[]运算符 [英] overloading [] operator

查看:84
本文介绍了重载[]运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图像这样重载[]运算符。


char * myclass :: operator [](char * str)

{

....

}


基本上我应该可以处理这些案件


char * c1 = myclass [" test"];


char * c2 =" test2";

myclass [" test''] = c2;


因此,myclass [" test"]可以出现在表达式的rhs或lhs中。在我的operator []函数中,必须以不同方式处理这些情况,我将如何区分它?

I am trying to overload [] operator like this.

char* myclass::operator[](char* str)
{
....
}

Basically I should be able to handle these cases

char* c1=myclass["test"];

char* c2="test2";
myclass["test'']=c2;

So, myclass["test"] can appear either in rhs or lhs of an expression. In my operator[] function which has to handle these cases differently, how will I differentiate it?

推荐答案


所以,myclass [" test"]可以出现在表达式的rhs或lhs中。在我的operator []函数中,必须以不同方式处理这些情况,我将如何区分它?
So, myclass["test"] can appear either in rhs or lhs of an expression. In my operator[] function which has to handle these cases differently, how will I differentiate it?



你不能。


operator []应该返回对要写入的数据的引用,如果你正在尝试创建一个由字符串索引的字符串数组,你选择了错误的类型,返回operator [],试试


string& myClass :: operator [](const char * str)

{

}

You can''t.

operator[] should return a reference to the data to be written, if you are trying to create an array of strings indexed by strings you have chosen the wrong type the the return of operator[], try

string &myClass::operator[](const char* str)
{
}



你不能。


operator []应该返回对要写入的数据的引用,如果你试图创建一个由字符串索引的字符串数组,你选择了错误的类型,返回运算符[],试试


string& myClass :: operator [](const char * str)

{

}
You can''t.

operator[] should return a reference to the data to be written, if you are trying to create an array of strings indexed by strings you have chosen the wrong type the the return of operator[], try

string &myClass::operator[](const char* str)
{
}



感谢您的更正。你的猜测是正确的。我正在尝试创建一个由字符串索引的字符串数组。我的问题是,只有在
myClass [s1] = s2的情况下,我才会为字符串创建一个占位符。 //为s1和s2创建内存


而不是


s3 = myClass [s1]; //没有创造任何东西。只返回对索引字符串的引用


因此,运算符在不同情况下的行为应该不同。这可能吗?

Thanks for the correction. And your guess is correct. I am trying to create an array of strings indexed by strings. My problem is that I will create a place holder for the strings only in case of

myClass[s1]=s2; //creates memory for s1 and s2

and not in case of

s3=myClass[s1]; //doesn''t create any thing. Just returns reference to indexed string

So the operator should behave differently in different cases. Is this possible?



myClass [s1] = s2; //为s1和s2创建内存


而不是


s3 = myClass [s1]; //没有创造任何东西。只返回对索引字符串的引用


因此,运算符在不同情况下的行为应该不同。这可能吗?
myClass[s1]=s2; //creates memory for s1 and s2

and not in case of

s3=myClass[s1]; //doesn''t create any thing. Just returns reference to indexed string

So the operator should behave differently in different cases. Is this possible?



在第二种情况下,您将能够分辨,因为当您查看索引s1时,无论您使用哪种方法进行存储,它都将存在。所以操作员这样做

In the second case you will be able to tell because when you look you look the index s1 in whatever method you are using for storage it will already exist. So the operator does this

展开 | 选择 | Wrap | 行号


这篇关于重载[]运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆