我想我知道你要去哪里了。 是的。它被称为升级。见拉科斯。 / david I''m hoping my post here doesn''t fall into the ''hard to say'' category,nonetheless I''ve been advised that multiple uses of accessor/mutator(get/set) member functions can be viewed as a ''design flaw''. In thatregard I''m trying to create an ''example'' class that''s an alternativeto the accessor/mutator approach. To further describe the problemconsider the class BAR (below) which has a member data in_use that FOOneeds visibility into. Similarily FOO has member data ''idx'' that BARneeds visibility into.In terms of visibility I could envision three approaches.1. Make the data members public2. declare BAR a friend of FOO and vise versa3. Use Get/Set. This presumably points to a design flaw.If memory serves me well, item 1 also suggests a ''design flaw'' so I''mleft with 2 but I''ve often been leery of ''friends''Any pointers/help appreaciated.Thanks# include<iostream># include "foo.h"class BAR{public:BAR() : in_use(0){//std::cout << " bar''s constructor " << std::endl;}~BAR() {}void GetPosFbkFoo(){in_use ^= 1;if (in_use){int jdx = foo.GetFbk();std::cout << jdx << std::endl;}else // do something else{foo.ComputeTorquerCmd();}}private:int in_use;// lots more member dataFOO foo;};int main(){BAR *ptrBar = new BAR;for (int idx(0); idx < 10; ++idx)ptrBar->GetPosFbkFoo();delete ptrBar;return 0;}# include "foo.h"# include <iostream>FOO::FOO() : idx(0){//std::cout << " foo''s constructor called " << std::endl;}FOO::~FOO(){std::cout << " foo destructing " << std::endl;}int FOO::GetFbk(){return ++idx; // for demo purposes}void FOO::ComputeTorquerCmd(){// need visibility into the in_use flag here.// Approach:// 1. Make the in_use flag public;// 2. declare BAR a friend of FOO// 3. Use Get/Set}#ifndef FOO_H#define FOO_Hclass FOO{public:FOO();~FOO();int GetFbk();void ComputeTorquerCmd();private:int idx;// more .. BAR};#endif[ Spam Prevention: Replace ma740988 with mpowell to email me .. ] 解决方案Use of set/get members is not necessarily a design flaw or the sign of poorcoding,if the setting of a property of the object causes some other side effect andthis is encapulated in the set method, then that is generally good. In theGUI programmingworld, widgets generally have a numer of getter/setters, yet setting a colorfor instance willnot only set the internal color variable, it will normally cause the widgetto be repainted inthe correct color.What is bad, is using a class as a data depository with the multitude ofsettters/getters,yet the client needs to perform the logic which should rightfully be done bythe class.With respect to your example, it doesn''t seem to have any connection with areal-worldsystem so I find it impossible to help with a suggestion to improve it.If you can state in simple words what you are trying to do, we might beable to help model it better in C++.daveI think I see where you''re going here.Yes. It''s called escalation. See Lakos. /david 这篇关于accessor / mutator - 设计'flaw'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!