Vectors Classes Private/Public

16,251

Solution 1

If it doesn't matter if someone comes along and empties the vector or rearranges all it's elements, then make it public. If it matters, then yes, you should make it protected/private, and make public wrappers like you have. [Edit] Since you say "it's a snake", that means it'd be bad if someone came and removed or replaced bits. Ergo, you should make it protected or private. [/Edit]

You can simplify a lot of them:

MyClass {
private:
     std::vector<int> _myints;
public:
     const std::vector<int>& get_ints() const {return _myints;}
     add_IntToMyints(int x){_myints.push_back(x));
};

That get_ints() function will allow someone to look at the vector all they want, but won't let them change anything. However, better practice is to encapsulate the vector entirely. This will allow you to replace the vector with a deque or list or something else later on. You can get the size with std::distance(myobj.ints_begin(), myobj.ints_end());

MyClass {
private:
     std::vector<int> _myints;
public:
     typedef std::vector<int>::const_iterator const_iterator;
     const_iterator ints_begin() const {return _myints.begin();}
     const_iterator ints_end() const {return _myints.end();}
     add_IntToMyints(int x){_myints.push_back(x));
};

Solution 2

For good encapsulation, you should keep your vector private.

Solution 3

Your question is not very concrete, so here's an answer in the same spirit:

Generally, your classes should be designed to express a particular concept and functionality. They should not just hand through another member class. If you find yourself replicating all the interface functions of a member object, something is wrong.

Maybe sometimes you really just need a collection of other things. In that case, consider a plain old aggregate, or even a tuple. But if you're designing a proper class, make the interface meaningful to the task at hand, and hide the implementation. So the main question here is, why do you need to expose the vector itself? What is its role in the class? What does its emptiness signify in terms of the semantics of your class?

Find the appropriate idioms and ideas to design a minimal, modular interface for your class, and the question might just go away by itself.

(One more idea: If for example you have some range-based needs, consider exposing a template member function accepting a pair of iterators. That way you leverage the power of generic algorithms without depending on the choice of container.)

Share:
16,251
Pella86
Author by

Pella86

I am a hobby programmer, I am a biologist, and I started with internet tutorial to approach the world of programming in python. After a while I've also learned a bit of C++. Here can be found the project I'm most proud of: Snake4d

Updated on June 04, 2022

Comments

  • Pella86
    Pella86 almost 2 years

    In C++ is always better to keep data of a class as private members.
    If a class has a vector as member is better to put it as a private or public member?

    If I have a vector as private member I cannot easily access to the member function of the vector. So I have to design the class with a method for every function I need to access the vector methods?

    Example given:

    class MyClass{
    private:
         std::vector<int> _myints;
    public:
         get_SizeMyints(){return _myints.size();}
         add_IntToMyints(int x){_myints.push_back(x));
    };
    

    or is better to keep the vector public and call MyClass._myints.push_back(x)?

    ---------------------edit--------------

    and just for clarity for what is needed this question:

    snake.h:

    enum directions{UP, DOWN, RIGHT, LEFT, IN, OUT, FW, RW };
    
    
    class Snake
    {
    private:
        enum directions head_dir;
        int cubes_taken;
        float score;
        struct_color snake_color;
        V4 head_pos;
    
    
    public:
    
        std::vector<Polygon4> p_list; //the public vector which should be private...
    
        Snake();
        V4 get_head_pos();
        Polygon4 create_cube(V4 point);
        void initialize_snake();
        void move(directions);
    
        void set_head_dir(directions dir);
        directions get_head_dir();
        void sum_cubes_taken(int x);
        int get_cube_taken();
    
        void sum_score(float x);
        float get_score();
    
        void set_snake_color();
    
    
    
    };
    

    so now I know how to change the code.

    btw... a question, if I need to copy the vector in an other class like this: GlBox.p_list = Snake.p_list (works if are private) what will be an efficent method if they where private?
    Running a for cycle to copy the the elements and pusshing back them in the GLBox.p_list seems a bit inefficent to me (but may be just an impression) :(