Class design with vector as a private/public member?

折月煮酒 提交于 2019-12-11 03:33:30

问题


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:


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.




回答2:


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.




回答3:


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!




回答4:


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)




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/197211/class-design-with-vector-as-a-private-public-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!