Vectors if are data of a class should be assigned as private member of that class. The class should provide the methods to access the vector methods needed.
Now I have my class Snake which encapsulate a snake for the classic game.
typedef std::vector<Polygon4>::const_iterator const_iterator;
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;
std::vector<Polygon4> p_list; //the vector
public:
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();
//vector manipulation functions
const_iterator p_list_begin() const {return p_list.begin();}
const_iterator p_list_end() const {return p_list.end();}
void add_IntToP_list(Polygon4 cube){p_list.push_back(cube);}
void clear_list(){p_list.clear();}
unsigned int get_list_size(){return p_list.size();}
};
I have an other class in my program, the graphic management:
class MyGLBox{
private:
std::vector<Polygon4> p_list;
public:
//do stuff...
//management vectors:
const_iterator p_list_begin() const {return p_list.begin();}
const_iterator p_list_end() const {return p_list.end();}
void add_IntToP_list(Polygon4 cube){p_list.push_back(cube););
void clear_list(){p_list.clear();}
unsigned int get_list_size(){return p_list.size();}
}
Now every frame in the game I need to copy the snake p_list into the MyGLbox p_list. If the vectors were public it would have been pretty easy:
myGLBox.p_list = snake.p_list;
now if they are private:
transfer_function(*MyGLBox box, *Snake snake){
const_iterator cube;
for(cube = snake->p_list_begin(); cube != snake->p_list_end(); cube++){
box->add_InTop_list(*cube);
}
}
is it right what I'm trying to do? Is there a better way to do it? To me the for cycle seems pretty inefficent