C++ code readability: use structs or inner lasses or something else?

Hi guys,

I’m currently making a c++ project and recently came up with a problem, that my code is becoming hard to read. I need to somehow group different parts of code into smaller subgroups for easier navigation (or at least only inside .h file).

I want to make something like this:
class UActor
{
group1
{
func1();
int var1;
}
gropu2
{
func2();
}
}

I found 2 ways to solve this problem - use structures or inner classes, but neither of them let me use variables from main class. Or maybe there is another solution?

Thanks

There are multiple ways, but if you simply want to split into subgroups you can use different namespaces. You can declare a namespace inside the class declaration and put related functions and variables inside it. Here’s an example:

class UActor
{
public:
    void func1();
    void func2();

    namespace Group1
    {
        void func3();
        int var1;
    }

    namespace Group2
    {
        void func4();
    }
};

You can then access these functions and variables using the scope resolution operator, like this:

UActor actor;
UActor::Group1::var1 = 42;
UActor::Group2::func4();
1 Like

Thanks!

1 Like