I have a c struct from old code that cannot be modified and I am trying to
write a wrapper C++ class around it.
This class is often passed as a pointer to some c functions of a library and I
wanted to keep my new class transparent and compatible with those functions.
That is, when using the code, I should be able to do either:
oldVector xyz;
function( &xyz );
myVector xyz;
function( &xyz );
My original implementation solved the issue using inheritance. That is, in
pseudo code:
// old code
typedef struct {float x, y, z;} oldVector;
/// my class
class myVector : public oldVector
{
/// new methods here
};
This worked fine for my needs, but I wanted to change myVector class to be a
template which would likely prevent the inheritance.
I know the alignment in the compilers I will be should result in an identical
memory layout.
I was wondering if it was possible to use type casting to work around it.
Basically, I was thinking something along the lines of:
template <class T> class Vec3
{
public:
T x, y, z;
// other methods....
};
class myVector : public Vec3< float >
{
operator oldVector();
// but for a pointer.... would this be valid????
operator *oldVector();
};
Would this be valid, or terrible ideas so far and something else is likely
better?
write a wrapper C++ class around it.
This class is often passed as a pointer to some c functions of a library and I
wanted to keep my new class transparent and compatible with those functions.
That is, when using the code, I should be able to do either:
oldVector xyz;
function( &xyz );
myVector xyz;
function( &xyz );
My original implementation solved the issue using inheritance. That is, in
pseudo code:
// old code
typedef struct {float x, y, z;} oldVector;
/// my class
class myVector : public oldVector
{
/// new methods here
};
This worked fine for my needs, but I wanted to change myVector class to be a
template which would likely prevent the inheritance.
I know the alignment in the compilers I will be should result in an identical
memory layout.
I was wondering if it was possible to use type casting to work around it.
Basically, I was thinking something along the lines of:
template <class T> class Vec3
{
public:
T x, y, z;
// other methods....
};
class myVector : public Vec3< float >
{
operator oldVector();
// but for a pointer.... would this be valid????
operator *oldVector();
};
Would this be valid, or terrible ideas so far and something else is likely
better?
Comment