views:

396

answers:

6

what is the best way to put a container class or a some other class inside a class as private or a public member?

Requirements:

1.Vector< someclass> inside my class

2.Add and count of vector is needed interface

+1  A: 

Whether a member is declared Private or Public depends entirely on your application. Could you give some more detail?

One important point to remember when declaring your member is that if you provide a "getter" to retrieve it, then you are no longer encapsulating that object. Instead, it can be good to write wrapper methods exposing only the functionality you wish to expose.

For example, with a Vector member, you might write an AddItem and Clear method, if that's all the functionality you wish to expose.

RB
A: 

Make all members private and use accessor methods, this allows you to change the implementation later. Only in very unusual circumstances would I make any data member public.

Remember that chaning the implementation happens more often than you may imagine, its not just a case of changing the type of the container but maybe you want to change the mechanism. Say you were storing names in a list, after a while you may chose to index this list with a hash and would like to have the hash updated every time you add a new name. If your implementation is suitably encapsulated doing this is easy, if you have just exposed the vector you would need to make changes that will adjust the interface (and so the change will ripple out).

If this is new to new you have a read of: http://en.wikipedia.org/wiki/Encapsulation_(classes_-_computers)

Scott James
A: 

There is a third way - sometimes it is better to inherit from the container and override it's methods to achieve your goal (for example thread safety). Anyway, making it public almost always isn't a good idea.

Dmitry Khalatov
STL containers do not have virtual destructors or virtual methods, so this may be dangerous territory.
bk1e
A: 

Considering that you want to encapsulate the container inside another class implies that it cannot be public, and also the public methods of your class should not expose anything implementation-specific about the container. That way the implementation of your class (i.e. the container) can be changed without changing its interface.

andreas buykx
A: 

Since you're talking about a class, I think it should be private. If you want it to be public, rather create a struct - to make it obvious that you want the members variables to be used.

A viable alternative to exposing the vector member is creating a visitor function (or an internal iterator). This way you obey the law of Demeter better:

class ContWrapper {
    std::vector<int> _ints;
public:
    class Action {
    public: 
        virtual void accept( int i ) = 0;
    };
    void each_int( Action& a );
};

Also be very careful when exporting e.g. an std::vector<T> from a library, too: the client code might not use the same STL implementation as you did, so the layout of these member variables may differ!

xtofl
+1  A: 

If the container's state is part of the class's invariant, then it should, if possible, be private.

For example, if the container represents a three dimensional vector then part of the invariant might be that it always contains exactly 3 numbers. Exposing it as a public member would allow code external to the class to change the containers size, which in turn could cause problems for any routine which requires the container's size to be constant. Keeping the container private limits the places in your software where the container's size can be modified to the class's member functions.

ejgottl