G
Gregory
I have a question about using STL containers in C++ class public
interface.
Lets say that I want to return some container from class method or
accept class method parameter as some container. For example:
class A
{
public:
const vector<int>& getTable() { return m_table; }
private:
vector<int> m_table;
};
In this case there is strong dependency between class A implementation
and its public interface. When I want to change the implementation by
replacing private vector<> container with list<> I'm forced to replace
the above
getTable() method with 'const list<int>& getTable()'
If clients of class A already used vector specific interface (random
access, for instance)
they will be affected by this change. Bad.
Typedef also does not help here.
class A
{
public:
typedef vector<int> IntContainer;
const IntContainer& getCont() { return m_cont; }
private:
IntContainer m_cont;
};
The IntContainer is still either vector or list with their entire
interface exposed.
Another example. I don't want to expose the whole container in the
public interface. I want to define an iterator to access my private
container entries.
class A
{
public:
typedef vector<int> IntContainer;
typedef IntContainer::iterator IntContIter;
IntContIter begin() { return m_cont.begin; }
IntContIter end() { return m_cont.end; }
private:
IntContainer m_cont;
};
Later I wanted to replace my private vector<> with list<>. I have
problem again.
Since vector<> exposed random access iterator but list<> has
bi-directional iterator.
Class A clients will be affected as before.
What can be done ? I can wrap every STL container with my own container
and expose only small common portion of all STL containers interface
(does it exist ???) in the class public interface and thus protect
clients from the class A implementation changes.
I can also wrap STL container iterators and expose the wrappers in the
public interface.
This seems to be too much work
Are there any other solutions for
these problems ?
What are accepted guidelines for using STL containers in class public
interface ?
Gregory
interface.
Lets say that I want to return some container from class method or
accept class method parameter as some container. For example:
class A
{
public:
const vector<int>& getTable() { return m_table; }
private:
vector<int> m_table;
};
In this case there is strong dependency between class A implementation
and its public interface. When I want to change the implementation by
replacing private vector<> container with list<> I'm forced to replace
the above
getTable() method with 'const list<int>& getTable()'
If clients of class A already used vector specific interface (random
access, for instance)
they will be affected by this change. Bad.
Typedef also does not help here.
class A
{
public:
typedef vector<int> IntContainer;
const IntContainer& getCont() { return m_cont; }
private:
IntContainer m_cont;
};
The IntContainer is still either vector or list with their entire
interface exposed.
Another example. I don't want to expose the whole container in the
public interface. I want to define an iterator to access my private
container entries.
class A
{
public:
typedef vector<int> IntContainer;
typedef IntContainer::iterator IntContIter;
IntContIter begin() { return m_cont.begin; }
IntContIter end() { return m_cont.end; }
private:
IntContainer m_cont;
};
Later I wanted to replace my private vector<> with list<>. I have
problem again.
Since vector<> exposed random access iterator but list<> has
bi-directional iterator.
Class A clients will be affected as before.
What can be done ? I can wrap every STL container with my own container
and expose only small common portion of all STL containers interface
(does it exist ???) in the class public interface and thus protect
clients from the class A implementation changes.
I can also wrap STL container iterators and expose the wrappers in the
public interface.
This seems to be too much work
these problems ?
What are accepted guidelines for using STL containers in class public
interface ?
Gregory