class A {
...
} // declaration A
A a1;
A a2;
a1.foo();
a1.variable = ...
a2 = a1;
What value should a2 has ?
memcpy(&a2, &a1, sizeof(a1)); so if there is pointer type member in class A, both a1 and a2 will have a pointer which is point to same object.
...
} // declaration A
A a1;
A a2;
a1.foo();
a1.variable = ...
a2 = a1;
What value should a2 has ?
- case 1: no override on operater=
memcpy(&a2, &a1, sizeof(a1)); so if there is pointer type member in class A, both a1 and a2 will have a pointer which is point to same object.
- case 2: override the operator=
本文探讨了C++中类实例赋值操作的两种情况:默认行为与重载等号运算符。分析了内存拷贝的过程及指针成员变量在不同情况下的表现。
1万+

被折叠的 条评论
为什么被折叠?



