Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "uninclude" a header file?

I have a class in the file AType.h and it is implemented in AType.cpp.

# include "PrivateType.h"

class AType{
    private:
    int a, b, c;
    PrivateType varX;

    public:
    ...
};

I want to use the class AType in the file main.cpp and I will need to include AType.h, but I want to avoid the inclusion of PrivateType.h in main.cpp.
I cannot create varX with malloc/new.
main.cpp have to know the size of AType at compile-time.

Current solution: (It is bad)

1 - Create a program to print sizeof(AType).
2 - Change the header:

# ifdef ATYPE_CPP
    # include "PrivateType.h"
#endif

class AType{
    private:
    # ifdef ATYPE_CPP
        int a, b, c;
        PrivateType varX;
    # else
        char data[ the size that was printed ];
    # endif

    public:
    ...
};

3 - And AType.cpp will start with:

# define ATYPE_CPP
# include "AType.h"

Edit 1

Is there a way or tool to automatically change a complex struct into C primitive types?
I do not want to have to open header files and find the structs.

If PrivateType is:

struct DataType {
    float a, b;
};
class PrivateType {
    void* a;
    int b;
    short c;
    DataType x;

    ... functions
};

AType would be changed to:

class AType {
    int a, b, c;
    struct x {
        void* a;
        int b;
        short c;
        struct x2{
            float a, b;
        };
    };
};

And I would handle copy/equality methods separately.
I use GCC or Clang.

Edit 2
A new solution?

It is for GCC.

1 - Get sizeof(AType) and __alignof__(AType).
2 - Change the header:

# ifdef ATYPE_CPP
    # include "PrivateType.h"
#endif

class AType{
    private:
    # ifdef ATYPE_CPP
        int a, b, c;
        PrivateType varX;
    # else
        char data[ 'the sizeof(AType)' ];
    # endif

    public:
    ...
}
# ifdef ATYPE_CPP
    ;
# else
    __attribute__ (( aligned( 'The __alignof__(AType)' ) ));
# endif

3 - Write all copy/equality methods in AType.cpp.

Will it work?

like image 899
Squall Avatar asked Mar 03 '26 13:03

Squall


2 Answers

Your current method will fail subtly and catastrophically. The compiler must see the same declaration for the class at all times. Specifically, consider the compiler-generated equality or assignment operator for class AType without the proper definition of PrivateType present. The compiler would incorrectly generate a copy/equality method for a char array.

What you can do is forward declare your private type:

class PrivateType;

class AType{
    private:
    int a, b, c;
    PrivateType *varX;

    public:
    ...
};

Notice that varX is now a pointer to a class that hasn't been defined yet (of course you must allocate/deallocate this yourself; a smart pointer type might help). In your AType.cpp you can #include "PrivateType.h" to get the full definition so you can actually use the members of the class.

like image 97
Greg Hewgill Avatar answered Mar 06 '26 02:03

Greg Hewgill


You cannot do what you want (because you ruled out dynamic allocation), and your "solution" does not work in general, even if you avoid the problems with compiler-generated special member functions which others mentioned. One problem is that types not only have a size, but also an alignment. For example, your real class contains an int, however your replacement class contains only a char array. Now on most platforms, int has alignment 4 (i.e. an int must be located at a 4 byte boundary), while char has aligment 1 (it cannot have any other alignment without violating the standard). That is, as soon as you try to create an object with your replacement definition, you risk getting it misaligned, which in the best case will cause a massive slowdown, in the worst case a crash of your program (and in the absolutely worst case, it will work in your tests, but fail when actually used).

like image 41
celtschk Avatar answered Mar 06 '26 02:03

celtschk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!