迁移 C++从结构到类的代码

发布于 2024-09-01 00:35:55 字数 207 浏览 8 评论 0原文

我正在将一些 C++ 代码从结构迁移到类。

我使用的结构主要用于位域优化,我不再需要它了(我现在更担心速度而不是节省空间)。

  • 进行此迁移的一般准则是什么?我仍处于规划阶段,因为这是一个影响代码主要部分的重大举措。我想在做任何事情之前先计划好一切。 我应该牢记哪些基本事项?

I am migrating some C++ code from structures to classes.

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).

  • What are the general guidelines for doing this migration? I am still in the planning stage as this is a very big move affecting a major part of the code. I want to plan everything first before doing it. What are all the essential things I should keep in mind?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桜花祭 2024-09-08 00:35:55

我无法说出所有基本的东西,但我可以说出一个:封装

C++ 中结构体和类之间的唯一技术区别是默认访问。在结构体中,默认情况下所有内容都是公开的;在课堂上,一切都是私人的。我假设您在这里谈论的是 POD 结构,其中所有内容都是公开的。

我要做的是:

  1. struct 关键字更改为 class 并查看调用代码在哪里中断。这会让您了解该类型的哪些部分在哪里使用。
  2. 由此,确定该类型的哪些元素应该是公共的,哪些应该是私有的。
  3. 为公共部分编写访问器函数,并更改调用代码以使用它们。
  4. 将需要访问私有部分的代码移到类本身中。

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. 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.

What I would do is this:

  1. Change the struct keyword to class and see where calling code breaks. That would give you a clue about what parts of the type are used where.
  2. From that, determine which elements of the type should be public, which should be private.
  3. Write accessor functions for the public parts, and change calling code to use them.
  4. Move the code that needs access to private parts into the class itself.
丿*梦醉红颜 2024-09-08 00:35:55

当将遗留代码库从 C 更新为 C++ 时,我的经验是,实际重新架构应用程序以将结构转换为传统 C++ 对象时,几乎没有什么价值,而且需要花费太多精力。因为毫无疑问,这就是你最终要做的事情。起初看起来似乎不是这样,但最终您会意识到您正在重新设计应用程序。

你没有充分说明你的目标是什么,所以也许这就是你的目标,但如果你只是想转换为 C++,那么应用程序中的新代码可以是 C++,只需重命名文件,添加一堆强制转换之前发生过 void* 的隐式转换,然后继续你的生活。

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. 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.

自此以后,行同陌路 2024-09-08 00:35:55

C++ 中的结构和类之间没有任何有意义的区别(它们仅在默认可见性方面有所不同)。除非您也想添加有意义的行为,否则我不会费心将结构迁移到类中。

There is no meaningful difference between structures and classes in C++ (they differ only by default visibility). I wouldn't bother to migrate the structures to classes unless you are going to add meaningful behaviors as well.

子栖 2024-09-08 00:35:55

首先,我将和其他人一起说,将所有代码从结构转移到类可能不是最好的举措。如果你想做得好(也就是说,不仅仅是用 class X { public: 更改 struct X {),这意味着重新设计应用程序(或多或少是一个完整的应用程序)。改写)。

这涉及引入新的错误、新的开发周期、额外的测试、更改文档等等。

第二,考虑到您可能有这样做的正当理由(对我来说“只是为了好玩”和“看看我是否能做到”在某些情况下可能是正当理由:D)这是我的答案针对您的问题:

1. What are the general guidelines for doing this migration?
2. What are all the essential things I should keep in mind?

要记住的准则和注意事项:

  • 在非常小的迭代中工作,并确保应用程序在迭代之间正常运行。如果您定义了单元测试,则可以通过它们进行工作(选择一个单元,按照一组步骤重新设计(见下文),然后调整并运行测试。

  • 选择代码的一个区域并完成它

  • 尝试遵循 每次更改的步骤如下:

    • 分析功能并重新设计
    • 与旧实现并行创建新实现
    • 在使用旧实现的地方切换到新实现
    • 测试应用程序是否仍然有效
    • 删除旧代码
    • 测试应用程序是否仍然有效
  • 如果您现在不这样做,开始使用分支源代码控制软件。没有什么比这更能削减它了。我推荐 Mercurial,但我知道 GIT 具有大致相同的功能。
    您稍后可以感谢我:o)。

  • 以事务方式执行更改(从一个区域开始并完成它,在第一个区域的更改进行到一半时不添加其他区域的更改)。如果您使用分支源代码控制和多个开发人员,则每个开发人员一次可以进行一个更改/区域,然后集中更改。

重构方法的优点:

  • 如果您中途认为所做的努力不值得(或者管理层认为不值得),应用程序将保持正常运行

  • 通过更改,应用程序的稳定性仍然是可管理的

如果你建立了一些里程碑,这应该是很容易管理的。

祝你好运!

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).

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:

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:

    • analyze functionality and redesign
    • create the new implementation in parallel with the old one
    • switch in the new implementation everywhere the old one is used
    • test that the application still works
    • remove the old code
    • test that the application still works
  • 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.
    You can thank me later :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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文