本文介绍了这是一个糟糕的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

推荐答案




为什么铁必须是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







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

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




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


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



为什么要将它用作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?



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.



为什么铁必须是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设计吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:18