D
doublemaster007
A(A &a) { memcopy(this &a, sizeof(A) ;}
Why we should not do this?? Is it because it spoils vtable??
Why we should not do this?? Is it because it spoils vtable??
A(A &a) { memcopy(this &a, sizeof(A) ;}
Why we should not do this?? Is it because it spoils vtable??
A(A &a) { memcopy(this &a, sizeof(A) ;}Why we should not do this?? Is it because it spoils vtable??
Perhaps. vtables are implementation-dependent, so it's best not to
risk it.
There's also the concern of whether A has any members with non-
trivial constructors. Consider this example:
struct B
{
char* pMem;
B()
{
pMem = new char[10];
}
~B()
{
delete[] pMem;
}
};
struct A
{
B m_BMember;
A(A &a)
{
memcpy(this, &a, sizeof(A));
}
};
If you instantiate A and delete it, you're fine; B's constructor and
destructor will behave normally. But if you instanciate A, and then
use your copy constructor to copy the contents of a second A, then
your B's member variables will be copied as well--meaning you now have
two instances of B which will each call the delete[] operator on the
same pointer when they're destroyed. And that's a serious bug.
vtable issues aside, unless you know with absolute certainty the
behavior of your base, derived and member classes, memcpy is *not* the
way to go. And even then, don't risk making a habit of it; There are
probably more gotchas that I haven't mentioned.
Perhaps. vtables are implementation-dependent, so it's best not to
risk it.There's also the concern of whether A has any members with non-
trivial constructors. Consider this example:struct B
{
char* pMem;
B()
{
pMem = new char[10];
}
~B()
{
delete[] pMem;
}
struct A
{
B m_BMember;
A(A &a)
{
memcpy(this, &a, sizeof(A));
}
If you instantiate A and delete it, you're fine; B's constructor and
destructor will behave normally. But if you instanciate A, and then
use your copy constructor to copy the contents of a second A, then
your B's member variables will be copied as well--meaning you now have
two instances of B which will each call the delete[] operator on the
same pointer when they're destroyed. And that's a serious bug.vtable issues aside, unless you know with absolute certainty the
behavior of your base, derived and member classes, memcpy is *not* the
way to go. And even then, don't risk making a habit of it; There are
probably more gotchas that I haven't mentioned.
Thank yo so much. If you keep vtable problem aside basically it does
the shallow copy. which otherwise dafault constructor does. Isnt it??
Memcopy is like bitwose copy, which compiler gen constructor otherwise
do. So i am not finding any problem here. Am i missing some thing?
Or is it that even compiler generated copy constructor has the same
problem in the example you have mentioned.
One more thing. I have read some were that problem i mentioned would
spoil vtables. ut i could not find how, why. Can any one pls explain
this?
A(A &a) { memcopy(this &a, sizeof(A) ;}
Why we should not do this?? Is it because it spoils vtable??
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.