operator [] - 返回引用 [英] operator[] -- returning references

查看:112
本文介绍了operator [] - 返回引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




让下标运算符返回

const引用变量被认为是不好的形式?如果不是,那么正确的方法是什么?

我的问题是由下面的代码提示的,我有问题的尝试

实现了一个返回const引用的下标运算符。

可疑代码最后标记。


< code>


// MyClass是一个大班我们不想复制很多

// MyClass有一个char getChar()功能

class MyClass;


class SubscriptTest {

private:

std :: vector< MyClass的> _vect;

public:

SubscriptTest(){};

void addObj(MyClass mc){_ vect.push_back(mc);}

const MyClass& operator [](char ch)const {return

lookUpByChar(ch);}

};


const MyClass& SubscriptTest :: lookUpByChar(char ch)const {

for(unsigned i = 0; i< _vect.size(); i ++){

if(_vect [i] .getChar()== ch)

返回i;

}


//问题在这里

return *(new MyClass); //返回值需要编译

}


< / code>


问题是什么查找失败时返回。如上所述,通过new创建一个

默认值似乎是错误的 - 它浪费了内存

并且需要跟踪创建的对象然后在
中销毁它们
下标'的析构函数。


另一种选择是在lookUpByChar函数中创建默认值为静态变量

或作为成员变量

SubscriptTest,并且该函数总是返回对

的引用。这对我来说似乎很合理,但我仍然不确定。


有没有人有任何想法/建议?


谢谢,

cpp

Hi,

Is it considered bad form to have the subscript operator return a
const reference variable? If not, what is the proper way to do it?
My question was prompted by the code below, my problematic attempt to
implement a subscript operator that returns a const reference. The
dubious code is marked at the end.

<code>

//MyClass is a large class we don''t want to copy alot
//MyClass has a "char getChar()" function
class MyClass;

class SubscriptTest {
private:
std::vector<MyClass> _vect;
public:
SubscriptTest() {};
void addObj(MyClass mc) {_vect.push_back(mc);}
const MyClass& operator[] (char ch) const {return
lookUpByChar(ch);}
};

const MyClass& SubscriptTest::lookUpByChar(char ch) const {
for (unsigned i=0; i<_vect.size(); i++) {
if (_vect[i].getChar() == ch)
return i;
}

//PROBLEM IS HERE
return *(new MyClass); //return value here needed to compile
}

</code>

The problem is what to return when the lookup fails. Creating a
default value via new, as I do above, seems wrong -- it wastes memory
and requires tracking the objects created and then destroying them in
Subscript''s destructor.

Another option would be to create the default value as static variable
inside the lookUpByChar function or as a member variable of
SubscriptTest, and have the function always return a reference to
that. That seems reasonable to me, but I still wasn''t sure.

Does anyone have any thoughts/suggestions?

Thanks,
cpp

推荐答案

cppaddict写道:
cppaddict wrote:


让下标操作符返回一个
const引用变量是否被认为是不好的形式?


不,不适用于const版本。这实际上是首选的方式。

如果没有,这样做的正确方法是什么?
我的问题是由下面的代码提示的,我有问题的尝试
实现一个返回const引用的下标运算符。最后标记了可疑的代码。

< code>

// MyClass是一个我们不想复制的大类
// MyClass有一个char getChar()函数
类MyClass;

类SubscriptTest {
私有:
std :: vector< MyClass> _vect;
public:
SubscriptTest(){};
void addObj(MyClass mc){_ vect.push_back(mc);}
const MyClass& operator [](char ch)const {return
lookUpByChar(ch);}
};

const MyClass& SubscriptTest :: lookUpByChar(char ch)const {
for(unsigned i = 0; i< _vect.size(); i ++){
if(_vect [i] .getChar()== ch)
返回i;
}

//问题在这里
返回*(新MyClass); //返回值需要编译
}

< / code>

问题是查找失败时返回的内容。如上所述,通过new创建一个
默认值似乎是错误的 - 它浪费了内存
并且需要跟踪创建的对象,然后在下标的析构函数中销毁它们。另一种选择是在lookUpByChar函数中创建默认值作为静态变量
或者作为SubscriptTest的成员变量,并且该函数总是返回对
那。这对我来说似乎很合理,但我仍然不确定。

有没有人有任何想法/建议?
Hi,

Is it considered bad form to have the subscript operator return a
const reference variable?
No, not for the const version of it. It''s actually the preferred way.
If not, what is the proper way to do it?
My question was prompted by the code below, my problematic attempt to
implement a subscript operator that returns a const reference. The
dubious code is marked at the end.

<code>

//MyClass is a large class we don''t want to copy alot
//MyClass has a "char getChar()" function
class MyClass;

class SubscriptTest {
private:
std::vector<MyClass> _vect;
public:
SubscriptTest() {};
void addObj(MyClass mc) {_vect.push_back(mc);}
const MyClass& operator[] (char ch) const {return
lookUpByChar(ch);}
};

const MyClass& SubscriptTest::lookUpByChar(char ch) const {
for (unsigned i=0; i<_vect.size(); i++) {
if (_vect[i].getChar() == ch)
return i;
}

//PROBLEM IS HERE
return *(new MyClass); //return value here needed to compile
}

</code>

The problem is what to return when the lookup fails. Creating a
default value via new, as I do above, seems wrong -- it wastes memory
and requires tracking the objects created and then destroying them in
Subscript''s destructor.

Another option would be to create the default value as static variable
inside the lookUpByChar function or as a member variable of
SubscriptTest, and have the function always return a reference to
that. That seems reasonable to me, but I still wasn''t sure.

Does anyone have any thoughts/suggestions?




抛出异常。



Throw an exception.


有没有人有任何想法/建议?
Does anyone have any thoughts/suggestions?




Rolf,


编译器是否需要返回语句无论?它似乎对我来说是
。我错过了什么吗?


谢谢,

cpp



Rolf,

Doesn''t the compiler require a return statement no matter what? It
seems to for me. Am I missing something?

Thanks,
cpp


cppaddict写道:
cppaddict wrote:
有没有人有任何想法/建议?
Does anyone have any thoughts/suggestions?



扔一个例外。



Throw an exception.



Rolf,

无论编译器是什么都不需要返回语句?



Rolf,

Doesn''t the compiler require a return statement no matter what?




如果无论如何都无法访问代码路径,请不要这样做。实际上,我的编译器。

你可以写:


const MyClass& SubscriptTest :: lookUpByChar(char ch)const {

******** for(unsigned i = 0; i< _vect.size(); i ++){

**************** if(_vect [i] .getChar()== ch)

********** **************返回i;

********}

********

抛出SomeException(找不到字符);

********返回MyClass(); //关闭编译器

}


是的,这意味着重新引用一个临时引用,但是自

它实际上从未实现过,它没有受到伤害。



Not if that code path can''t be reached anyway. Actually, my compiler.
You could just write:

const MyClass& SubscriptTest::lookUpByChar(char ch) const {
********for (unsigned i=0; i<_vect.size(); i++) {
****************if (_vect[i].getChar() == ch)
************************return i;
********}
********
throw SomeException("character not found");
********return MyClass(); //shut the compiler up
}

Yes, this would mean reuturning a reference to a temporary, but since
it''s never actually reached, it doesn''t hurt.


这篇关于operator [] - 返回引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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