Deriving from a STL container

T

tragomaskhalos

I'm not intending to make a snyde comment so I apologize if it comes
across that way, but it seems to me that you are arguing for increased
coupling between objects for the sake of saving yourself some key-
strokes. For me, I try and always use the weakest relationship between
objects that I can get away with, not saying this is "the way" just
that is what I try to do.. Since in this example we know that the std
container will not have a) and protected data that I need to use, and
b) the std container contains no virtual functions I need to override
then I will use containment, forwarding functions have never been a
chore to write or get right. If you choose to inherit in this case, it
appears that you would be creating a needless dependency.

Your desire for the weakest relationships is exactly correct I
think, but if you contain a vector and delegate to it, and I inherit
privately from vector and make its operations available via
"using", it seems to me we are *both* tightly coupled to the vector.

However it turns out that Sutter (Exceptional C++ item 15) agrees
with you, and recommends using private inheritance "only when
absolutely necessary". So I'm not going to argue the point, but
I need to digest his and your arguments before I can convince
myself!
 
S

shazled

You shouldn't need to rewrite "tons of code", a standard practice is
to use composition and make an adapter class "have a" std container,
plus whatever extra functionality you need to extend the container for
in the first place.

Oops, I may have done something dodgy recently -- although it did seem
OK at the time. Is the following dangerous?

// How a time series is represented
template <typename T>
struct TimeSeries : public vector<T>
{
double noise; // An estimate of the noise variance
double delta; // The time between samples
};

I was too lazy to wrap a vector, wanted most of the vectors
functionality and felt the following was cumbersome to use:

template <typename T>
struct TimeSeries
{
vector<T> series;
double noise;
double delta;
};

Should I have gone with the second option or is there a better way?

Saul
 
V

Victor Bazarov

Oops, I may have done something dodgy recently -- although it did seem
OK at the time. Is the following dangerous?

// How a time series is represented
template <typename T>
struct TimeSeries : public vector<T>
{
double noise; // An estimate of the noise variance
double delta; // The time between samples
};

I was too lazy to wrap a vector, wanted most of the vectors
functionality and felt the following was cumbersome to use:

template <typename T>
struct TimeSeries
{
vector<T> series;
double noise;
double delta;
};

Should I have gone with the second option or is there a better way?

In both cases 'series' member (or the base class) is exposed in such
a way that it can be updated without any change to 'noise' or 'delta'.
I would think that you want to keep 'series' (and the rest) private
so any update to it goes under your control so you can update the
other values accordingly...

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top