   Trouble With Returning A Pointer
  I have a simple class that holds a vector of pointers to objects. I need to create a function to get a single pointer. I gave it a shot but I get a breakpoint when it hits it because apparently my pointer is not valid. What's the problem?bool SimShaderManager::Shader(UINT p_index, SimShader *p_outShader){if(InBounds(p_index)){vector<SimShader*>::iterator itr = shaders.begin();itr += p_index;p_outShader = (*itr);return true;}return false;}...SimShader *shader = NULL;shaderManager.Shader(mesh.ShaderID(), shader);shader->SetTechnique("DiffuseLighting"); //the breakpoint happens in here when it tries to change data in the pointer
  Not a bump, just an update and another question.If I were to use the function in the following way, should it also work?SimShader shader;shaderManager.Shader(mesh.ShaderID(), &shader);shader.SetTechnique("DiffuseLighting");;  The problem is you're only adjusting the value of a variable local to the SimShaderManager::Shader function.  You have a pointer.  You make a copy of that pointer, which you send to the function.  You then modify that copy.  The original is never changed.  You need an additional level of indirection so that you can modify the original pointer:bool SimShaderManager::Shader(UINT p_index, SimShader * & p_outShader)Σnigma   ;  Both are working "propertly," niether will work the way you want.You want to change the value of p_OutShader (the value of a pointer is the address it points to) from within Shader(). To do this you must pass it by reference (either as double pointer or a reference to a pointer), otherwise you are not changing the value (address pointed to) of the pointer at the calling scope.Edit: Enigma beat me. I hang my head in shame.   ;  The pointer is passed by value, meaning that the function uses a copy of the original pointer. Using that copy, you can affect the variable that the pointer points to, but not the original pointer itself. If you want the modifications brought to a function parameter to be reflected in the variable that your passed to the function, you need to pass it by reference: bool SimShaderManager::Shader(UINT p_index, SimShader* &p_outShader)On the other hand, shaderManager.Shader(mesh.ShaderID(), &shader); will not work: the pointer you pass to the function is a temporary variable. Changing the address that pointer points to will in no way affect shader itself. In fact, C++ should stop you from doing so: temporary variables can only be passed by const reference.   ;Quote:Original post by NickGravelynNot a bump, just an update and another question.If I were to use the function in the following way, should it also work?SimShader shader;shaderManager.Shader(mesh.ShaderID(), &shader);shader.SetTechnique("DiffuseLighting");No.Your function can change a pointer, or assign a variable.If you want to do either, you have to write two functions.  Possibly you can simply override, but that can lead to confusion and bugs.  I would advise picking one of the two (providing access to pointers to shaders, or providing the ability to copy over a provided shader), but not both, unless you have a dire need for both.(your original post question was answered by the previous responders);  All the rest of you SimShaders are just imitating...   