Although I have tested I'm not sure if it will always work:
class Val {
public:
int data[4];
};
int main() {
int data[4];
Val* v = reinterpret_cast<Val *>(data);
}
Although I have tested I'm not sure if it will always work:
class Val {
public:
int data[4];
};
int main() {
int data[4];
Val* v = reinterpret_cast<Val *>(data);
}
Rodrigo, the most guaranteed way to successfully convert from array to structure is to create a structure builder and an assignment operation. Thus you determine how it is made and avoids problems. Also, anyone reading the code will know how the conversion happens. Here's an example code.
class Val {
void __construct( int *dataArray ){
data[0] = dataArray[0] ;
data[1] = dataArray[1] ;
data[2] = dataArray[2] ;
data[3] = dataArray[3] ;
}
void __destruct( void ){
}
public:
int data[4] ;
~Val( void ){
__destruct() ;
}
Val( int *dataArray ){
__construct(dataArray) ;
}
Val& operator=( int *dataArray ){
__destruct() ;
__construct(dataArray) ;
return *this ;
}
} ;
int main() {
int data[4] ;
Val v1=data , v2 ;
v2 = data ;
}
As for reinterpret_cast
, this is not data conversion, but pointers to read the binary data code, reading as if they were data of another type. I have information that this works yes, it was as if you were creating a union that handles pointer to int
and pointer to structure composed of only fields of type int
, type like.
union Pointer {
int *dataPointer ;
Val *valPointer ;
} ;
int main() {
int data[4] ;
Val *v ;
/* v = reinterpret_cast<Val*>(data) ; */ {
Pointer p ;
p.dataPointer = data ;
v = p.valPointer ;
}
}
Any questions?