Search code examples
c++migrationclassstructurecode-migration

migrating C++ code from structures to classes


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?

Solution

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