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?
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?