这是一个糟糕的const设计吗? [英] Is this bad const design?

查看:60
本文介绍了这是一个糟糕的const设计吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






我有一个对象,让我们称之为HydraulicPress


class HydraulicPress

{

public:


void setIron(iron * iron)const;

Iron * getIron(void)const;


void pressIron(void)const;


private:


mutable铁*铁;

};


注意setIron()的const!那是因为我希望能够使用

一个HydraulicPress作为const对象,同时它在不同的

对象Iron上运行。我不能省略get / set-methods,因为印刷机和熨斗

将紧张耦合一段时间。


这是不好的设计?有没有人认识到这个设计问题

并且可以推荐一些类似的设计模式或思维方式?

最好的问候

Daniel Marcus


Hi,

I have an object, let''s call it HydraulicPress

class HydraulicPress
{
public:

void setIron( Iron* iron ) const;
Iron* getIron( void ) const;

void pressIron( void ) const;

private:

mutable Iron* iron;
};

Notice the const at setIron()! That''s because I want to be able to use
a HydraulicPress as a const object, meanwhile it operates on a different
object Iron. I can''t omit the get/set-methods since the press and iron
will be tight coupled for a while.

Is this bad design? Is there anyone who recognize this design problem
and could recommend some similar design pattern or way of thinking?
Best Regards
Daniel Marcus

推荐答案



" DeMarcus" <无**** @ tellus.orb>在留言中写道

news:40 ************** @ tellus.orb ...

"DeMarcus" <no****@tellus.orb> wrote in message
news:40**************@tellus.orb...

你好,

我有一个对象,让我们称之为HydraulicPress

类HydraulicPress
公开:

void setIron (铁*铁)const;
铁* getIron(void)const;

void pressIron(void)const;

私人:

mutable Iron * iron;
};

请注意setIron()中的const!


由于setIron()改变了HydraulicPress对象的状态,它应该是
不是常量。

这是因为我希望能够使用一个HydraulicPress作为const对象


为什么要将它用作const对象,同时你想要

来改变那个对象的状态?不应该是非常量的对象吗?

,同时它运行在不同的对象Iron上。我不能省略get / set-methods,因为印刷机和熨斗将紧密耦合一段时间。


const函数getIron()返回一个非const指针。一般来说,暴露内部数据成员并不是一个好主意。

这个糟糕的设计是什么?


我倾向于这么认为。

有没有人认识到这个设计问题
并且可以推荐一些类似的设计模式或思维方式?

Hi,

I have an object, let''s call it HydraulicPress

class HydraulicPress
{
public:

void setIron( Iron* iron ) const;
Iron* getIron( void ) const;

void pressIron( void ) const;

private:

mutable Iron* iron;
};

Notice the const at setIron()!
Since setIron() changes the state of the HydraulicPress object it should
not be const.
That''s because I want to be able to use
a HydraulicPress as a const object
Why do you want use it as a const object, while at the same time you want
to change the state of that object? Shouldn''t the object be non-const?
, meanwhile it operates on a different
object Iron. I can''t omit the get/set-methods since the press and iron
will be tight coupled for a while.
The const function getIron() returns a non-const pointer. In general it is
not a good idea to expose internal data members like this.
Is this bad design?
I am inclined to believe so.
Is there anyone who recognize this design problem
and could recommend some similar design pattern or way of thinking?




为什么铁必须是HydraulicPress类的参数?为什么不用

将铁作为参数传递给pressIron()函数?


class HydraulicPress

{

public:

void pressIron(Iron& iron)const;

};

-

Peter van Merkerk

peter.van.merkerk(at)dse.nl





Why does iron have to be a parameter of the HydraulicPress class? Why not
pass iron as a parameter to the pressIron() function?

class HydraulicPress
{
public:
void pressIron(Iron& iron) const;
};
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl






" DeMarcus" <无**** @ tellus.orb>在留言中写道

news:40 ************** @ tellus.orb ...

"DeMarcus" <no****@tellus.orb> wrote in message
news:40**************@tellus.orb...

你好,

我有一个对象,让我们称之为HydraulicPress

类HydraulicPress
公开:

void setIron (铁*铁)const;
铁* getIron(void)const;

void pressIron(void)const;

私人:

mutable Iron * iron;
};

请注意setIron()中的const!那是因为我希望能够使用一个HydraulicPress作为const对象,同时它可以在不同的对象Iron上运行。


不知怎的,我不明白这一点。你能详细说明为什么这是必要的吗?恕我直言,set方法不应该是const,而get方法应该是
。此外,你应该知道你的get方法向你的数据成员暴露了

内部,这很可能不是一个好主意,

除非你有充足的理由去做所以。

我不能省略get / set-methods因为印刷机和熨斗
会紧耦合一段时间。

这是不是很糟糕设计?有没有人认识到这个设计问题
并且可以推荐一些类似的设计模式或思维方式?

Hi,

I have an object, let''s call it HydraulicPress

class HydraulicPress
{
public:

void setIron( Iron* iron ) const;
Iron* getIron( void ) const;

void pressIron( void ) const;

private:

mutable Iron* iron;
};

Notice the const at setIron()! That''s because I want to be able to use
a HydraulicPress as a const object, meanwhile it operates on a different
object Iron.
Somehow I don''t get the sense of this. Could you elaborate why this is
necessary? IMHO the set method should not be const, whereas the get method
should be. Furthermore you should be aware that your get method exposes the
internals to your data member which is most probably not a very good idea,
unless you have a good reason to do so.
I can''t omit the get/set-methods since the press and iron
will be tight coupled for a while.

Is this bad design? Is there anyone who recognize this design problem
and could recommend some similar design pattern or way of thinking?




你的液压机是否需要有一个成员对象这是通过

get / set方法设置的。难道你不能通过熨斗,这个熨斗目前在按压功能的
中处理。此外,您可能会考虑启用更多

通用设计(以及命名),因为可能需要按压&能够
处理其他材料但铁。


HTH

Chris



Does your hydraulic press need to have a member object which is set via the
get/set methods. Can''t you pass the iron, which is currently processed in
the pressIron function. Furthermore you might consider enabling a more
general design (and also naming) as the press might be required & able to
process other materials but iron.

HTH
Chris





Peter van Merkerk写道:


Peter van Merkerk wrote:
" DeMarcus" <无**** @ tellus.orb>在消息中写道
新闻:40 ************** @ tellus.orb ...
"DeMarcus" <no****@tellus.orb> wrote in message
news:40**************@tellus.orb...

<我有一个对象,让我们称之为HydraulicPress

类HydraulicPress
公共:

void setIron(铁*铁) )const;
Iron * getIron(void)const;

void pressIron(void)const;

私人:

可变铁* iron;
};

注意setIron()的const!
Hi,

I have an object, let''s call it HydraulicPress

class HydraulicPress
{
public:

void setIron( Iron* iron ) const;
Iron* getIron( void ) const;

void pressIron( void ) const;

private:

mutable Iron* iron;
};

Notice the const at setIron()!



因为setIron()改变了HydraulicPress对象的状态所以应该
不是常量。


Since setIron() changes the state of the HydraulicPress object it should
not be const.

那是因为我希望能够使用一个HydraulicPress作为const对象
That''s because I want to be able to use
a HydraulicPress as a const object



为什么要将它用作const对象,同时又想要改变对象的状态?难道这个对象不应该是非const的吗?


Why do you want use it as a const object, while at the same time you want
to change the state of that object? Shouldn''t the object be non-const?

,同时它对不同的对象Iron进行操作。我不能省略get / set-methods,因为按下和熨斗
会紧耦合一段时间。
, meanwhile it operates on a different
object Iron. I can''t omit the get/set-methods since the press and iron
will be tight coupled for a while.



const函数getIron()返回一个非 - 指针。一般来说,暴露这样的内部数据成员并不是一个好主意。


The const function getIron() returns a non-const pointer. In general it is
not a good idea to expose internal data members like this.

这个设计不好吗?



我倾向于这么认为。


I am inclined to believe so.

有没有人认识到这个设计问题
并且可以推荐一些类似的设计模式或思维方式?
Is there anyone who recognize this design problem
and could recommend some similar design pattern or way of thinking?



为什么铁必须是HydraulicPress类的参数?为什么不把铁作为参数传递到pressIron()函数?

类HydraulicPress
公共:
void pressIron(Iron& iron) const;
};

-
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Why does iron have to be a parameter of the HydraulicPress class? Why not
pass iron as a parameter to the pressIron() function?

class HydraulicPress
{
public:
void pressIron(Iron& iron) const;
};
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl




是的,你的所有部分都是正确的。这就是为什么我很困惑。

请在回答Chris Theis的时候阅读更多内容。


~Daniel




Yes, you''re right in all parts. That''s why I''m so confused.
Please read more in my response to Chris Theis.

~ Daniel



这篇关于这是一个糟糕的const设计吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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