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?