const函数问题...... [英] const functions problems...

查看:74
本文介绍了const函数问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何避免在下面的两个运营商

[]实施中编写和维护相同的代码?我知道在下面的最小例子中,

实现是微不足道的,但我发现了这种类型的问题(保持两个

函数重载一个const而一个不是const - 有同一个身体),很多

次......


谢谢,

罗穆卢斯

#包括< iostream>


类SomeClass

{

public:

int& operator [](int index)

{

返回数组[index];

}


const int& operator [](int index)const

{

返回数组[index];

}


protected:

//内部数据

int array [10];

};


void func(const SomeClass& sc,int index)

{

std :: cout<< sc [index]<< std :: endl;

}


void main(无效)

{

SomeClass c ;

c [3] = 5;

func(c,3);

}

How can I avoid writting and maintaining the same code in the two operator
[] implementation below? I know that in the minimal example below, the
implementation is trivial, but I found this type of problem (maintaining two
function overloads one const and one not const - having the same body), many
times...

Thanks,
Romulus
#include <iostream>

class SomeClass
{
public:
int & operator[](int index)
{
return array[index];
}

const int & operator[](int index) const
{
return array[index];
}

protected:
// internal data
int array[10];
};

void func(const SomeClass & sc, int index)
{
std::cout << sc[index] << std::endl;
}

void main(void)
{
SomeClass c;
c[3] = 5;
func(c, 3);
}

推荐答案



" Romulus Mare" <莫**************** @ yahoo.com>在消息中写道

news:bh ************ @ ID-1317.news.uni-berlin.de ...

"Romulus Mare" <mo****************@yahoo.com> wrote in message
news:bh************@ID-1317.news.uni-berlin.de...
如何我可以避免在下面的两个运算符
[]实现中编写和维护相同的代码吗?我知道在下面的最小例子中,
实现是微不足道的,但是我发现了这种类型的问题(维护
两个函数重载一个const而另一个不是const - 具有相同的主体),$ b $很多次......

谢谢,
Romulus
How can I avoid writting and maintaining the same code in the two operator
[] implementation below? I know that in the minimal example below, the
implementation is trivial, but I found this type of problem (maintaining two function overloads one const and one not const - having the same body), many times...

Thanks,
Romulus




有时一个选项是使用const_cast,所以const版本调用

非const版本。这是合法的,前提是非const版本不会在执行期间修改对象(通常是这种情况)。


class X

{

const int& func()const {return const_cast< X *>(this) - > func(); }

int& func()

{

...

}

};


john



Sometimes an option is to use a const_cast, so the const version calls the
non-const version. This is legit provided the non-const version doesn''t
modify the object during its execution (which is often the case).

class X
{
const int& func() const { return const_cast<X*>(this)->func(); }
int& func()
{
...
}
};

john


有时候选项是使用const_cast,所以const版本调用
非const版。这是合法的,前提是非const版本在执行期间不会修改对象(通常是这种情况)。

类X
{
const int& func()const {return const_cast< X *>(this) - > func(); }
int& func()
{
...
}
};
Sometimes an option is to use a const_cast, so the const version calls the
non-const version. This is legit provided the non-const version doesn''t
modify the object during its execution (which is often the case).

class X
{
const int& func() const { return const_cast<X*>(this)->func(); }
int& func()
{
...
}
};




谢谢,约翰...


所以,如果我理解正确,我将面临以下其中一个非常危险的解决方案:


1。在两个函数中保持相同的代码:一个const和一个不是const

- 或者 -

2.接受使用编译器而不检查非常量中的常量;

int& FUNC()。我知道,代码是一样的,不应该在课堂上做任何修改,但是这不比老式C演员更优雅...


解决此类问题的其他选择吗?


Romulus



Thanks, John...

So, if I understood correctly, I am facing one of the following very
dangerous solutions:

1. Maintain the same code in two functions: one const and one not const
- OR -
2. Accept to work with the compiler not checking of constness in "non const"
int& func(). I know, the code is the same and should not do any
modifications in class, but this is no more elegant than old-style C cast...

Any other options for solving such issues?

Romulus


Romulus Mare写道:
Romulus Mare wrote:
如何避免在下面的两个
[] []实现中编写和维护相同的代码?我知道在下面的最小例子中,
实现是微不足道的,但是我发现了这种类型的问题
(保持两个函数重载一个const而另一个不是const -
具有相同的主体)很多次......


我见过人们为此使用了const_cast(我唯一知道的地方是
知道它不在哪里保证签名错误)。

#include< iostream>

类SomeClass
{
公开:
int& operator [](int index)
{
返回数组[index];
}

const int& operator [](int index)const
{
返回数组[index];
}


const int& operator [](int index)const

{

return const_cast< SomeClass *>(this) - > operator [](index);

}


有了这个,你不需要保留两份实现副本。

当然,你不应该修改实现中的对象。

protected:
//内部数据
int array [10];
};

void func (const SomeClass& sc,int index)
{
std :: cout<< sc [index]<< std :: endl;
}

主要(无效)


主_must_返回int。

{
SomeClass c;
c [3] = 5;
func(c,3);
}
How can I avoid writting and maintaining the same code in the two
operator
[] implementation below? I know that in the minimal example below, the
implementation is trivial, but I found this type of problem
(maintaining two function overloads one const and one not const -
having the same body), many times...
I''ve seen people making use of const_cast for this (the only place I
know where it isn''t a guaranteed sign for an error).
#include <iostream>

class SomeClass
{
public:
int & operator[](int index)
{
return array[index];
}

const int & operator[](int index) const
{
return array[index];
}
const int & operator[](int index) const
{
return const_cast<SomeClass*>(this)->operator[](index);
}

With this, you don''t need to maintain two copies of the implementation.
Of course, you shouldn''t modify the object in that implemention.
protected:
// internal data
int array[10];
};

void func(const SomeClass & sc, int index)
{
std::cout << sc[index] << std::endl;
}

void main(void)
main _must_ return int.
{
SomeClass c;
c[3] = 5;
func(c, 3);
}






这篇关于const函数问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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