Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6532989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:04:15+00:00 2026-05-25T10:04:15+00:00

Vectors if are data of a class should be assigned as private member of

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:04:15+00:00Added an answer on May 25, 2026 at 10:04 am

    If you already have a function

    void add_IntToP_list(Polygon4 cube){p_list.push_back(cube););
    

    for adding a single element, you can add another overload for adding a range of elements

    void add_IntToP_list(vector<Polygon4>::const_iterator first, 
                          vector<Polygon4>::const_iterator last)
    { p_list.insert(p_list.end(), first, last); }
    

    and then call that with the range you want to add, maybe

    box->add_IntToP_list(snake->p_list_begin(), snake->p_list_end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that should hold a reference to some data, without owning
I want to have a class with a private static data member: class C
Say you have a simple class with some storage data structure (list, vector, queue,
I have a large nested vector that look like this: import Data.Vector let x
I have a class called SparseMatrix which contain a private vector of type Cell.
Is it possible to implement a function to access a private data vector foo
Suppose I have a class like this: class MyClass { private: vector<MyOtherClass> myMember; public:
Suppose I have something like this: class Collection { private: typedef std::vector< std::shared_ptr<Something> >::iterator
So I have two vectors of data points, and I would like to make
I have a problem with my iOS application. I receive binary vector data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.