By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,132 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,132 IT Pros & Developers. It's quick & easy.

Containing containers - handling interface ?

P: n/a
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?
Jul 23 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.