Hello
I'm not new to C++ programming, but I have a question for you guys and gals :) So the code below compiles fine (due to that ugly workaround in main func), but that makes d_copy.field2 unitialized (0xCC bytes in MSVC). How would you change the code to make d equal to d_copy? You can't change the type of the global variable and you must use it to copy d. However, you can use pointers, references and whatever pleases you. I guess set and get functions are optional, but I'd love to see them too. Is that possible in C++? If not, please let me know! ;)
class CBase
{
public:
CBase() : field1(0) {}
~CBase() {}
int field1;
};
class CDerived :
public CBase
{
public:
CDerived() : field2(0) {}
~CDerived() {}
int field2;
};
CBase b;
void func_set(CBase *pb)
{
b = *pb;
}
CBase func_get()
{
return b;
}
int main()
{
CDerived d;
CDerived d_copy;
d.field1 = 321;
d.field2 = 123;
func_set(&d);
d_copy = *(CDerived*)(&func_get());
return 0;
}
If the question is unclear to you, please let me know.