Containing containers - handling interface ?

S

SpOiLeR

Hello!

I have something like this:

class A;
Class B;

typedef std::list<A> ContainerA;
typedef std::vector<B> ContainerB;

class C
{
....
private:
ContainerA ac;
ContainerB bc;
}

Class C is not "kind of" ContainerA, nor "kind of" ContainerB. I want to
say, containment is more logical than inheritance (even though, as I've
heard, std containers were not meant to be inherited from, so if only for
that, inheritance is out of question). Now, what really interests me, how
to create good interface of class C? This needs to be accomplished in a way
that some of goodies from ContainerA and ContainerB interface are not lost.

I have two working solutions. First is to put methods into class C
interface that really only return iterators and stuff like that from
ContainerA and ContainerB. For example:

class C
{
public:
typedef ContainerA::iterator iteratorA;
typedef ContainerB::const_iterator const_iteratorA;

iteratorA begin() { return ac.begin(); }
const_iterator_A end() { return ac.end(); }
...// And so on for anything I need.
private:
ContainerA ac;
ContainerB bc;
};

Second would be this:

class C
{
public:
ContainerA ac;
ContainerB bc;
}

From the point of good design which is better? Is this one of the (rare
and mythical) cases where data members should be exposed?
 

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
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top