无关类中函数的通用调用接口 [英] Common calling interface to functions in unrelated classes

查看:75
本文介绍了无关类中函数的通用调用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

从VC6迁移到VS2005后,我正在重构一些古老的代码,想知道是否有一种方法可以解决以下情况:

有两个相关但独立的类,它们包含具有相同名称和参数的函数,分别称为ClassA和ClassB.主代码包含 Classa ClassB的实例,并且该代码散落在以下情况中:

Hi all,

I''m refactoring some ancient code following a move from VC6 to VS2005, and wonder if there''s a way of tidying up the following situation:

There are two related but independent classes containing functions with identical names and parameters, call them ClassA and ClassB. The main code contains an instance of either ClassA or ClassB, and the code is littered with the following situation:

if (pClassA)
    pClassA->function();
else if (pClassB)
    pClassB->function();


如果两个类具有相同的基类,则可以执行以下操作:


If the two classes had the same base class I could do something like:

BaseClass* GetPointer()
{
    if ( pClassA ) return pClassA;
    else if ( pClassB ) return pClassB;
}


并在主要代码中执行:


and in the main code do:

GetPointer()->function();


但他们没有,所以我不能.
因此,您的十个起点是这样的:是否有一种简化方法,所以我可以删除重复的if-else块,并执行与类具有相同基类但调用时可以调用函数的方式类似的操作>不添加一个新的包含虚拟版本的function()?


but they don''t, so I can''t.
So, your starter for ten is this: is there a way of streamlining this so I can remove the repeated if-else blocks and do something similar to the way the functions can be called if the classes have the same base class, but without adding a new common base class containing a virtual version of function()?

推荐答案

的通用基类,这根本没有任何意义.您可能会错过任何东西.请根据您的讲话考虑以下情形.

This does not make a sense at all. You might have missed something by any chance. Consider the following scenario based on what you said.

Class A {
  DoStuff1();
  DoStuff2();
  DoStuff3();
}

Class B {
  DoStuff1();
  DoStuff2();
  DoStuff3();
}



现在,如果我实例化这两个类



Now if I instantiate both classes

A *pClassA = new A();
B *pClassB = new B();



告诉我如何使用pClassA调用B的方法?你不能.我认为代码正在执行的工作是检查A和B的实例是否有效.当然,如果pClassA pClassB 为空,您也不想打电话,还是吗?



Tell me how can you call B''s methods using pClassA? you can''t. I think what the code is doing is it is checking the instance of A and B are valid. Of course you don''t want to call if either pClassA or pClassB are null, or do you?

if (pClassA)
    pClassA->function();
else if (pClassB)
    pClassB->function();




至于重构代码,我相信可以做些事情.您熟悉设计模式吗?我建议研究一下这本Scott Meyers设计模式书 http://www.amazon.com/More-Effective-Improve-Programs-Designs/dp/020163371X/ref=pd_bxgy_b_img_b [ ^ ]




As far as refactoring the code, I am sure there could something that can be done. Are you familiar with design patterns? I suggest look into this Scott Meyers design pattern books http://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876/ref=sr_1_5?s=books&ie=UTF8&qid=1300375803&sr=1-5[^] and http://www.amazon.com/More-Effective-Improve-Programs-Designs/dp/020163371X/ref=pd_bxgy_b_img_b[^]


免责声明:我不喜欢发布第二个答案,除非它是radi老茧与第一个不同.但是在这种情况下,讨论需要更多空间和解释,因此我将其发布为答案.

您说的是:是的,因此每次调用其中一个函数时,都必须检查哪个实例化了.

不,不,不.您弄错了.用if(pClassA)if(pClassB)检查不会告诉您它是哪种类型.它仅检查指针是否有效(即实例化,句点)

以这个为例

Disclaimer: I don''t like to post second answer unless it is radically different from the first. But in this case the discussion needs more space and explanation, so I am posting it as Answer.

You said: Yes, so every time it calls one of the functions it has to check which one is instantiated.

No, No, No. You are getting it wrong. checking with if(pClassA) or if(pClassB) does not tell you which type it is. It only checks if the pointer is valid (i.e it is instantianted, period)

Take this for example

A *pClassA = new A();
A *pClassB = new A();
B *pClassC = new B();

if (pClassA)
  //Do something
else if (pClassB)
  //Do different thing
else if (pClassC)
  // Do entirely different thing



您是否正在检查那里的类型,否.它只是验证它是否为无效状态.换句话说,等于说



Do you thing it is checking for the type there, No. It only validates it is instatiated. In other words it is equivalent to saying

if (null != pClassA)
  //Do something
else if (null != pClassB)
  //Do different thing
else if (null != pClassC)
  // Do entirely different thing



要检查类型,您必须调用typeid

if (typeid(pClassA) == typeid(A*))

您会发现我怀疑原始程序员是否正在检查类型,而是他们正在检查对象是否已实例化并且不为null.



To check for type you have to call typeid

if (typeid(pClassA) == typeid(A*))

you see I doubt the original programmer is checking for the type, rather they are checking the object is instantiated and is not null.


如果这些类共享一些方法名(及其签名),它们实际上实现了相同的接口,因此我将显式创建这样的接口并使两个类从其派生.
If the classes share some method names (and their signatures), they, de facto, implement the same interface, hence I would explicitly create such an interface and make the two classes derive from it.


这篇关于无关类中函数的通用调用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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