[英]migrating C++ code from structures to classes
I am migrating some C++ code from structures to classes. 我正在将一些C ++代码从结构迁移到类。
I was using structures mainly for bit-field optimizations which I do not need any more (I am more worried about speed than saving space now). 我使用的结构主要用于比特场优化,我不再需要(我现在更担心速度而不是节省空间)。
I can't name all the essential things, but I can name one: encapsulation . 我不能说出所有基本的东西,但我可以说出一个: 封装 。
The only technical difference in C++ between struct and class is the default access. 结构和类之间C ++的唯一技术差异是默认访问。 In a struct, everything is public by default;
在结构中,默认情况下一切都是公共的; in a class, everything is private.
在课堂上,一切都是私密的。 I'm assuming that you're talking about POD structs here, where everything is public.
我假设你在这里谈论POD结构,一切都是公开的。
What I would do is this: 我会做的是:
struct
keyword to class
and see where calling code breaks. struct
关键字更改为class
并查看调用代码中断的位置。 That would give you a clue about what parts of the type are used where. When updating a legacy codebase from C to C++, my experience is there is very little value and entirely too much effort involved in actually rearchitecting your application to convert structures to traditional C++ objects. 在将遗留代码库从C更新到C ++时,我的经验是,实际重新架构应用程序以将结构转换为传统的C ++对象时,实际上没有太大的价值和太多的努力。 Because make no mistake, thats is what you will end up doing.
因为毫无疑问,这就是你最终会做的事情。 It won't seem it at first, but eventually you'll realize you're redesigning the application.
起初它似乎不会,但最终你会意识到你正在重新设计应用程序。
You don't say enough about what your goals are, so maybe this is your goal, but if you're just trying to convert to C++ so new code in your app can be C++, just rename the files, add a bunch of casts where implicit conversions from void* were occurring before, and move on with your life. 你没有说明你的目标是什么,所以也许这是你的目标,但如果你只是想转换为C ++,那么你的app中的新代码可以是C ++,只需重命名文件,添加一堆强制转换来自void *的隐式转换发生在之前,并继续你的生活。
There is no meaningful difference between structures and classes in C++ (they differ only by default visibility). C ++中的结构和类之间没有任何有意义的区别(它们仅在默认可见性方面不同)。 I wouldn't bother to migrate the structures to classes unless you are going to add meaningful behaviors as well.
我不打算将结构迁移到类,除非你要添加有意义的行为。
First , I will join the others and say that moving all the code from structures to classes may not be the best move. 首先 ,我将加入其他人并说将所有代码从结构转移到类可能不是最好的举措。 If you were to do it well (that is, more than just changing
struct X {
with class X { public:
) that means redesigning the application (more or less a complete rewrite). 如果你做得好(也就是说,不仅仅是更改
struct X {
with class X { public:
:),这意味着重新设计应用程序(或多或少是完全重写)。
This involves introducing new bugs, new development cycles, extra testing, changing documentation and so on. 这涉及引入新的错误,新的开发周期,额外的测试,更改文档等。
Second , considering you may have valid reasons for doing this (for me "just for fun" and "to see if I can do it" can be valid reasons in some situations :D) here are my answers to your questions: 其次 ,考虑到你可能有正当理由这样做(对我而言“只是为了好玩”和“看看我是否可以做到”在某些情况下可能是有效理由:D)以下是我对你问题的回答:
1. What are the general guidelines for doing this migration?
2. What are all the essential things I should keep in mind?
Guidelines and things to keep in mind: 指南和要记住的事项:
work in very small iterations , and make sure the application is functional between iterations. 在非常小的迭代中工作 ,并确保应用程序在迭代之间起作用。 If you have unit-tests defined, you can work your way through them (pick one unit, redesign following a set of steps (see below), then adapt and run the tests.
如果您定义了单元测试,您可以通过它们(选择一个单元,按照一组步骤重新设计(见下文)),然后调整并运行测试。
pick one area of your code and finish it . 选择代码的一个区域并完成它 。
try to follow these steps for each change: 尝试对每个更改执行以下步骤:
If you're not doing it at the moment, start using a branching source-control software . 如果您现在没有这样做,请开始使用分支源代码控制软件 。 Nothing less quite cuts it.
没有什么比这更能减少了。 I recommend Mercurial, but I understand GIT has about the same features.
我推荐Mercurial,但我理解GIT具有相同的功能。 You can thank me later :o).
你以后可以感谢我:o)。
Perform changes transactionally (start with one area and finish it, without adding changes from other areas while the changes for the first one are half-way through). 以事务方式执行更改 (从一个区域开始并完成更改,而不添加其他区域的更改,而第一个区域的更改是中途)。 If you use a branching source-control and multiple developers you can have one change/area per developer at a time, then centralize the changes.
如果您使用分支源代码控制和多个开发人员,则每个开发人员可以同时拥有一个更改/区域,然后集中更改。
Advantages of a refactoring methodology: 重构方法的优点:
the application stays functional if you decide half-way through that the effort is not worth it (or if management decides the effort is not worth it) 如果您决定中途付出的努力是不值得的(或者如果管理层认为努力不值得),那么该应用程序将保持正常运行
the stability of the application remains manageable through the changes 应用程序的稳定性仍然可以通过更改来管理
If you establish some milestones this should be quite manageable. 如果你建立一些里程碑,这应该是非常易于管理的。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.