Confirmed users
1,345
edits
Nnethercote (talk | contribs) |
Nnethercote (talk | contribs) |
||
| Line 110: | Line 110: | ||
=== An Example Involving Inheritance === | === An Example Involving Inheritance === | ||
Things are a little trickier when inheritance is involved. Every sub-class needs to have its own implementation of <tt>SizeOfExcludingThis</tt>. An example: | Things are a little trickier when inheritance is involved. Every sub-class needs to have its own implementation of <tt>SizeOfExcludingThis</tt>. In contrast, if <tt>SizeOfExcludingThis</tt> is present, only a single <tt>SizeOfIncludingThis</tt> is needed for the whole class hierarchy. An example: | ||
class B { | class B { | ||
| Line 118: | Line 118: | ||
SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const | SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const | ||
{ | { | ||
... | return ... // measure things pointed to by B-specific fields | ||
} | |||
virtual size_t | |||
SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const | |||
{ | |||
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); | |||
} | } | ||
}; | }; | ||
| Line 139: | Line 145: | ||
* <tt>D::SizeOfExcludingThis</tt> must also call <tt>B::SizeOfExcludingThis</tt>, to ensure that any objects pointed to by fields inherited from <tt>B</tt> are measured. | * <tt>D::SizeOfExcludingThis</tt> must also call <tt>B::SizeOfExcludingThis</tt>, to ensure that any objects pointed to by fields inherited from <tt>B</tt> are measured. | ||
* If <tt>SizeOfIncludingThis</tt> is called on a <tt>D</tt> object, control will end up in <tt>B::SizeOfIncludingThis</tt>, but its <tt>SizeOfExcludingThis()</tt> call will end up calling <tt>D::SizeOfExcludingThis</tt> (because <tt>B::SizeOfIncludingThis</tt> is <tt>virtual</tt>), even if the call was invoked via a <tt>B*</tt> pointer. | |||
* The <tt>NS_MUST_OVERRIDE</tt> is an annotation that indicates that this function must be overridden by any sub-classes. It's used by some static analysis tools. Currently these tools aren't running, but they might in the future, and it's a useful piece of documentation. | * The <tt>NS_MUST_OVERRIDE</tt> is an annotation that indicates that this function must be overridden by any sub-classes. It's used by some static analysis tools. Currently these tools aren't running, but they might in the future, and it's a useful piece of documentation. | ||
* The <tt>MOZ_OVERRIDE</tt> annotation says that <tt>D::SizeOfExcludingThis</tt> overrides the corresponding function in <tt>B</tt>. On supporting compilers it gets expanded to the <tt>override</tt> keyword. It helps prevent accidentally having small differences in the function signatures (e.g. forgetting a <tt>const</tt>) that prevent the overriding from happening | * The <tt>MOZ_OVERRIDE</tt> annotation says that <tt>D::SizeOfExcludingThis</tt> overrides the corresponding function in <tt>B</tt>. On supporting compilers it gets expanded to the <tt>override</tt> keyword. It helps prevent accidentally having small differences in the function signatures (e.g. forgetting a <tt>const</tt>) that prevent the overriding from happening. | ||
=== Other Considerations === | === Other Considerations === | ||