S
Suzanne Vogel
Hi,
Given: I have a class with protected or private data members, some of
them without accessor methods. It's someone else's class, so I can't
change it. (eg, I can't add accessor methods to the parent class, and I
can't make some "helper" class a friend of the parent class to help in
accessing the data.)
Problem: I want to derive a class that has a copy constructor that
properly copies those data members.
What I tried:
1. I tried using memcpy() to copy all the data members. That always
crashed if the derived class contained virtual functions (?!), and
sometimes even more times than that.
class Derived
{.....
public:
Derived(Parent& rhs)
{
memcpy(this, &rhs, sizeof(Parent));
}
}
*** What gives? Why doesn't this work?
I'm guessing I'm not copying rhs into the proper portion of Derived's
memory layout. :-/
2. I tried static_casting the parent class to the derived class, so that
I could gain access to its data members directly.
class Derived
{.....
public:
Derived(Parent& rhs)
{
dataMemberX = static_cast<Derived&>(rhs).dataMemberX;
}
}
*** Is this *safe*? ie, Is it safe to static_cast from one instance down
to a *derived* class, even if that instance wasn't originally created of
the derived type?
Seems to me that sometimes this will work (as it has every time I've
*tried* it), and sometimes I'll get a mem access exception because the
derived class is bigger than the parent class.
3. *** Anyone have better suggestions for what else I might try? ;-)
Hacks are welcome, as long as they're "safe".
Thanks!
Suzanne
Given: I have a class with protected or private data members, some of
them without accessor methods. It's someone else's class, so I can't
change it. (eg, I can't add accessor methods to the parent class, and I
can't make some "helper" class a friend of the parent class to help in
accessing the data.)
Problem: I want to derive a class that has a copy constructor that
properly copies those data members.
What I tried:
1. I tried using memcpy() to copy all the data members. That always
crashed if the derived class contained virtual functions (?!), and
sometimes even more times than that.
class Derived
{.....
public:
Derived(Parent& rhs)
{
memcpy(this, &rhs, sizeof(Parent));
}
}
*** What gives? Why doesn't this work?
I'm guessing I'm not copying rhs into the proper portion of Derived's
memory layout. :-/
2. I tried static_casting the parent class to the derived class, so that
I could gain access to its data members directly.
class Derived
{.....
public:
Derived(Parent& rhs)
{
dataMemberX = static_cast<Derived&>(rhs).dataMemberX;
}
}
*** Is this *safe*? ie, Is it safe to static_cast from one instance down
to a *derived* class, even if that instance wasn't originally created of
the derived type?
Seems to me that sometimes this will work (as it has every time I've
*tried* it), and sometimes I'll get a mem access exception because the
derived class is bigger than the parent class.
3. *** Anyone have better suggestions for what else I might try? ;-)
Hacks are welcome, as long as they're "safe".
Thanks!
Suzanne