开发者

C++ where I'll need local extension or foreign method?

I'm not familiar with C++ enough to know where those 2 very similar refactorings (intro开发者_JAVA技巧duce local extension and introduce foreign method) will be needed. I know cases that forces to use those, but I can't figure sample classes that will need this "trick". I'm making C++ refactoring tutorial, to help new developers, but it looks like I need yours help too :)

If you used it, please tell me in where and why. Thanks in advance.


"Foreign" methods are methods that operate on, but aren't members of a class. In Java, that just means (static) methods of other classes that take the first class as an argument. In C++, you'll need to be aware of free functions. Unlike Java , in C++ methods can exist outside classes. int main() is the most famous one. The STL is also packed with free functions. Just have a peek in alone.

For Java, the site you linked advises that "foreign methods are a work-around" and suggest "Introduce Local Extension" as a alternative refactoring. In C++, free functions are not a work-around. That means there's much less pressure to introduce local extensions.

Furthermore, in C++ functions are not virtual by default, and objects are often passed by value. This means that the "introduce local extensions" technique often fails.

I hope this goes to show the fundamental problem with your approach: you're taking refactoring techniques for Java, which address specific Java weaknesses, and try to convert them into C++ which has a rather different set of weaknesses. Even where it works, it's often unnecessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜