Daniel <
daniel...@gmail.com> writes:
> On Friday, August 26, 2016 at 5:23:09 PM UTC-4, Mr Flibble wrote:
>> Some of the idiotic things that regulars of this newsgroup still,
>> presumably, continue to advocate:
>>
>> 3) Never derive from standard containers despite the fact that interface
>> augmentation is useful.
>
> For what reason would you want to derive from standard containers? Interface
> augmentation? Use free functions.
I've done it for things like (simplified):
class CartesianCoord3 : public std::vector<double,3>
{
public:
double& x(void) { return operator[](0);}
double& y(void) { return operator[](1);}
double& z(void) { return operator[](2);}
};
It means you get all the iterator types for free without having to
declare / define them. The key is never let ownership of the object
belong to a pointer to the base class (so the virtual destructor is not
an issue).
Another thing that I've found helpful is using trivial inheritance for
type safety:
/// [0], [1], [2] are (x,y,z)
class CartesianCoord3 : public std::vector<double,3>{
};
/// [0], [1], [2] are (r,phi,psi)
class PolarCoord3 : public std::vector<double,3>{
};
Although the objects are identical, I can't pass a CartesianCoord3 to a
function that expects a PolarCoord3, or vice versa. Or I can define
explicit conversion operators between the two (Here Be Dragons).
Actual technical question:
if I inherit from base class with a non-virtual destructor, but don't add
any members am I OK destroying the object through a pointer-to-base?
What if I don't add any data members (i.e. interface augmentation only)?
What if I add only POD data members?