-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Pete Becker wrote:
Think in terms of implementation here: the constructor for a type sets
the vtable pointer to point to the vtable for that type. When you've
stomped on the vtable pointer with new(this)Foo(x) you do not have a Bar
object. You have a mongrel, with Bar's data and Foo's vtable pointer.
But technically, no, there's no undefined behavior here. There's no
behavior at all, since there's no code being executed. You'd have to
create and object, call operator=, and then do something with that object.
class Foo {
public:
// default constructor
Foo();
// l-value copy constructor
Foo(const Foo &);
// destructor
virtual ~Foo();
// assignment operator
virtual Foo &operator=(const Foo &x) {
if(this != &x) {
// disable virtual mechanism
this->Foo::~Foo();
new(this) Foo(x);
}
return *this;
}
private:
// private data omitted
};
class Bar : public Foo {
public:
// default constructor
Bar() : Foo(), a(), b() {
}
// l-value copy constructor
Bar(const Bar &x) : Foo(x), a(x.a), b(x.b) {
}
// destructor
~Bar() {
}
// assignment operator
Bar &operator=(const Bar &x) {
// assign base class subobject
Foo::operator=(x);
// assign member subobjects
a = x.a;
b = x.b;
return *this;
}
// other methods omitted
private:
int a;
int b;
};
int main() {
Bar b, c;
c = b; // UB?
}
So, is there any better method to write Foo::operator=(const Foo &) without
causing problem on Bar::operator=(const Bar &) if it is not changed?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEARECAAYFAksmR48ACgkQG6NzcAXitM+8iwCgh32S4LQu2Kz0TVp3BIDfhM99
fG4An1AXOmMSpajSTT0/9s9EFlHEQFPp
=8mDm
-----END PGP SIGNATURE-----
include <new>, missing definitions of Foo's constructor and destructor.
Then call this method as the last step in the main(). When you compile