   [C++] Using Const For Outbound Parameters
  I need a specific function that I'm not sure how to (or if I even can) create. I have a STL vector with a bunch of SimMesh* objects. I need to be able to get a pointer to a single SimMesh* object. So I need a pointer to a pointer. The tricky part is that I was wondering if it's possible that the returned pointer cannot manipulate the original data. Can this be done? Here's what I think it would look like, but I'm not sure if this would work.#include <vector>using std::vector;vector<SimMesh*> meshes;...void FillData(UINT i, SimMesh**& value const){   (*value) = meshes;}...SimMesh* mesh;FillData(&someNumber);mesh->SomeConstFunction(); //this should work. something like drawing the meshmesh->ChangeSomeVariable(); //this should NOT work. i want to prevent this external pointer from changing any values
  Why not just return a const reference from your function.  Just avoid pointers.const SimMesh& FillData(UINT i){   return *meshes;};  Hm. Never did quite think of that. Using that method, would my external reference simply be a copy of the data? I don't want a copy because the meshes get manipulated internally and I want the external variable to reflect those changes. Also, is it possible to use that technique with an outbound parameter? I need the function to return bool or not because it does some bounds checking and things like that.   ;  I would use SimMesh * as return value, can still do your bounds checking, it would just return null on an invalid index. You don't want a pointer to a pointer that is in a vector. When the vector resizes and stuff now those **'s are invalid.   ;  Return a pointer if it's possible you might want to return NULL as an error; otherwise, you can return a const reference. For example:const SimMesh &FillData();...const SimMesh &myMesh = FillData();myMesh.constMethodHere(); //OKmyMesh.nonConstMethod(); //ERROR;  I agree that it's better it's better to just return the object (regardless of reference or pointer) for the trivial case of the code you posted.  It's much simpler and more natural.But if you want to do it via out parameter anyway the only trick is that you should return a pointer to a const object.  e.g.:class foo;void FillData(int i, const foo ** value){    (*value) = 0;}void bar(){    const foo * p;    FillData(0, &p);    // works, note 'const foo *'    foo * p2;    FillData(0, &p2);   // error, note 'foo *'};Quote:Original post by NickGravelynHm. Never did quite think of that. Using that method, would my external reference simply be a copy of the data? I don't want a copy because the meshes get manipulated internally and I want the external variable to reflect those changes. Also, is it possible to use that technique with an outbound parameter? I need the function to return bool or not because it does some bounds checking and things like that.The returned const reference would reference the object pointed to by the pointer in your vector.  There are no copies made.  A const reference is better than a pointer because you can guarantee you can't change the object referenced.  You could instead return a pointer-to-const, but using a reference you never have to worry about things like inadvertently deleting the object or whether is "const SimMesh*" or "SimMesh* const".As for exceptional conditons like bounds checking, there are always exceptions.The one disadvantage is if yur vector resized, the reference may become invalid.   ;  Const rules in C++:Read from right to left.  References are non-constable.char const ** x; // pointer to a pointer to a const characterchar * const * y; // pointer to a cosnt pointer to a non-const characterchar ** const z; // const pointer to a non-const pointer to a non-const characterSo, what you want is:class SimMesh {  public:    void reading_method() const;    void writing_method();};void FillData(UINT i, SimMesh const** value const){   if (*value) *value = meshes;}...SimMesh const* mesh;FillData(2, &mesh);if (!mesh) PANIC();mesh->reading_method(); // worksmesh->writing_method(); // fails to compile;  Thanks! That is just what I wanted to find.   ;Quote:Original post by DrEvilI would use SimMesh * as return value, can still do your bounds checking, it would just return null on an invalid index. You don't want a pointer to a pointer that is in a vector. When the vector resizes and stuff now those **'s are invalid.What do you mean, exactly? My vector is full of pointers. I need a pointer to an object in the vector, which inevitably means I have a pointer to a pointer. Here's what it looks like:class SimMesh{//stuff};...vector<SimMesh*> meshes