Originally Posted by
manasij7479
Works?
Do you realize that the pVoid s can be anything when you copy stuff into locations pointed by it?
Right. Obviously I wasn't thinking. Still, it's easily fixed.
Code:
#include <iostream>
#include <cstring>
struct A {
int n;
};
class B {
public:
A* a1;
void* pVoid;
};
void func(B *b) {
A a;
a.n = 2;
b->pVoid = new A;
std::memcpy(b->pVoid, (void*)&a, sizeof(A));
std::cout << ((A*)(b->pVoid))->n << '\n';
delete (A*)b->pVoid;
}
int main() {
A a;
a.n = 1;
B b;
b.pVoid = new A;
std::memcpy(b.pVoid, (void*)&a, sizeof(A));
std::cout << ((A*)(b.pVoid))->n << '\n';
delete (A*)b.pVoid;
func(&b);
}