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