   help with data encapsution in C++
  I'm trying to seperate video code from logic so that the video part has read only access to relevant information, but I don't entirely know how to do it properly in c++.See the following example, i want to let the video section call Ship::get_position() on every ship, but not let it call Ship::move().Any help about the best way to do this would be much appreciated. "logic.hpp":struct Position{float x,y;};class Ship{public:void move ();Position * get_position ();private:Position pos;};class World{public:void run_logic (struct GameFrame * gameFrame)//calls Ship::move() for all shipsprivate:std::vector<Ship> ships;};"video.hpp":class Video{public:int refresh_screen ();//calls Ship::get_position() for all shipsprivate:SDL_Surface *screen;};
  I think that you've got it around the wrong way. A class that handles rendering surfaces and the drawing the screen shouldn't know anything about game objects. At the least it should know about the surface that you want to be rendered and at the most perhaps a wrapper around the surface (perhaps containing clipping and colour key information, etc.).I'd suggest creating a render() function within Video and perhaps a separate class for handling the rendering of ships (maybe ShipView). That way, the world can create a ShipView object and pass each Ship to its render() function. The ShipView class will know how to render each ship and can pass a surface the Video::render().struct Position{float x,y;};class Ship{public:void move ();Position * get_position ();private:Position pos;};class ShipView{public:    virtual void render(Video& video, const Ship& ship)    {   // perhaps a vector of loaded ship surfaces is stored...   // find the surface that corresponds to this ship by   // looking at the details of the particular ship.   video.render(ship_sfc);    }};class World{public:void run_logic (struct GameFrame * gameFrame)void render(Video& video)   {  // call ship_view.render() for each ship.   }private:   ShipView ship_view;std::vector<Ship> ships;};class Video{public:int refresh_screen ();void render(SDL_Surface* sfc);private:SDL_Surface *screen;};;Use const. You can mark certain functions (such as getting the position) as having no side-effects on the object, while others (such as moving the object) do have side-effects. Then, if some code manipulates an object by constant reference, that code will only be able to call those functions that are marked as const.If some code only needs to know that an object is something with a position, you don't need to tell it that it's actually a ship. This involves creating a SomethingWithPosition interface implemented by Ship, and having the code expect a reference to a SomethingWithPosition that will actually be a Ship.;  If it is a small game and not a large scale object, try thisclass Ship{public:void draw(const &Video) const;}void Ship::draw(const &Video renderer) const{// x,y and sprite are member datarenderer.draw(x_, y_, sprite_);}One thing about OOP is that it's not just encapsulation of data but also functionality as well. One thing that is arguable is whether the ship should draw itself, or it should be passed to the renderer to be drawn. If it is the other way round, it's better to take the previous posts' advice and create an interface for all drawable objects which has x,y position. But for a simple project the above should suffice. Right now having the ship able to draw itself may make the interface bloated over the long run.At any rate, the Ship should also inherit from an interface which defines draw()   ;Quote:Original post by ToohrVykUse const. You can mark certain functions (such as getting the position) as having no side-effects on the object, while others (such as moving the object) do have side-effects. Then, if some code manipulates an object by constant reference, that code will only be able to call those functions that are marked as const.If some code only needs to know that an object is something with a position, you don't need to tell it that it's actually a ship. This involves creating a SomethingWithPosition interface implemented by Ship, and having the code expect a reference to a SomethingWithPosition that will actually be a Ship.Even better is for it to expect a PositionComponent of which Ship provides. Even better would be for it to accept a const Vector3& or whatever, and get on with writing the damn game [grin].   