c++ big 3 when subclassing

Update — new best practice using smart pointers would have mostly trivial or empty implementations of the big3.

See post on [[virtual — only 1 of the big 3 please]]

When you create a

class C : public B

copier — Please call B copier in C copier’s initializer
assignment — Please call B assignment among first lines in C assignment overload
dtor — no need to call B’s dtor. When reclaiming C , compiler guarantees to reclaim the B part of the C object real estate, but imagine a ptr field in B points to a 22 byte object. The 4-byte ptr is in the C object real estate, but not those 22 bytes. Compiler guarantees to reclaim the 4 byte ptr but you must make it reclaim the 22 bytes.

Note most of B’s public methods are inherited, but not big 3. These 3 methods wouldn’t do a good job if inherited, as they need access to subclass fields.

Just as in java, constructors aren’t inherited. You can inherit everything from your mother, but not her date of birth.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s