无效 SetSomeValue(int i);实例成员函数跟踪谁使用 SetSomeValue() 设置 SomeValue 成员变量
我想知道是否有任何合适的设计模式可用。
当 CMove 更新步骤时,我的情况如下所示
class CMove {
public:
void SetSpeed(float fSpeed) { m_fSpeed = fSpeed; }
void update() { /* do somthing with fSpeed */}
float m_fSpeed;
};
Class CSomeEvent_1 {
void OnEvent() { m_Move.SetSpeed(10.f);}
CMove m_Move;
};
Class CSomeCallBack_1 {
void OnCallback() { m_Move.SetSpeed(20.f);}
CMove m_Move;
};
,如果 m_fspeed 的 CMove 得到了我不想要的错误变量。 所以我想找出什么对象将其设置为 false。但是存在许多不同角色的实例,它们具有 CMove 类的成员变量。
首先简单的方法是
void SetSpeed(float fSpeed, char* file, int line) { // do log where's calling from }
m_Move.SetSpeed(10.f, __FILE__, __LINE__);
的床
我认为这是我想要发送
void SetSpeed(SendObject pObj, float fSpeed) { m_latestSettingObj = pObj; m_fSpeed = fSpeed; }
,如下所示有没有合适的设计模式可用?
或任何建议都会有所帮助,
谢谢。本亨
i was wondering there is any appropriate design pattern available.
my situation is something like below
class CMove {
public:
void SetSpeed(float fSpeed) { m_fSpeed = fSpeed; }
void update() { /* do somthing with fSpeed */}
float m_fSpeed;
};
Class CSomeEvent_1 {
void OnEvent() { m_Move.SetSpeed(10.f);}
CMove m_Move;
};
Class CSomeCallBack_1 {
void OnCallback() { m_Move.SetSpeed(20.f);}
CMove m_Move;
};
when the CMove update step, if CMove of m_fspeed got wrong variable that i don't want to.
so i want to find out what object set it false. but there's a lot of different role of instance existing that have CMove class of membervariable.
at first simple way is
void SetSpeed(float fSpeed, char* file, int line) { // do log where's calling from }
m_Move.SetSpeed(10.f, __FILE__, __LINE__);
i think it's bed
i want to send like below
void SetSpeed(SendObject pObj, float fSpeed) { m_latestSettingObj = pObj; m_fSpeed = fSpeed; }
is there any appropriate design pattern available?
or any sugguestion will be helpful
thanks. bonhyoung
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,你想跟踪 SetSpeed 的调用位置
例如 CSomeEvent_1 或 CSomeCallback_1 以及设置的速度。
您在示例中使用了多个 CMove 实例,但为了跟踪,您需要所有事件/回调的一些通用结构。
一种方法是在 CMove 中拥有一个静态列表,并使用 SetSpeed(SendObject..) 成员函数来跟踪谁在调用以及设置了多少速度。该列表可以是循环列表以避免内存溢出 - 不确定调用 SetSpeed 的频率?
或者您想直接检测何时设置了无效值,那么 hansmaad 建议抛出包含调用者信息的异常将是更好的方法。
If I understood you correctly you want to keep track of from where SetSpeed has been called
e.g. CSomeEvent_1 or CSomeCallback_1 and what speed was set.
You are using several CMove instances in your example but in order to keep track you would need some common structure for all the events/callbacks.
One approach would be to have a static list in CMove and use your SetSpeed(SendObject..) member function to keep track of who is calling and what speed was set. The list could be a circular list to avoid overflowing memory - not sure how often SetSpeed is called?
Or do you want to directly detect when an invalid value was set, then hansmaad's suggestion of throwing an exception containing information about the caller would be the better way.