• Advertisement
Sign in to follow this  

[C++] Using Const For Outbound Parameters

This topic is 4288 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 mesh
mesh->ChangeSomeVariable(); //this should NOT work. i want to prevent this external pointer from changing any values

Share this post


Link to post
Share on other sites
Advertisement
Why not just return a const reference from your function. Just avoid pointers.

const SimMesh& FillData(UINT i)
{
return *meshes;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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(); //OK
myMesh.nonConstMethod(); //ERROR

Share this post


Link to post
Share on other sites
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 *'
}

Share this post


Link to post
Share on other sites
Quote:
Original post by NickGravelyn
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.


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.

Share this post


Link to post
Share on other sites
Const rules in C++:

Read from right to left. References are non-constable.


char const ** x; // pointer to a pointer to a const character
char * const * y; // pointer to a cosnt pointer to a non-const character
char ** const z; // const pointer to a non-const pointer to a non-const character


So, 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(); // works
mesh->writing_method(); // fails to compile

Share this post


Link to post
Share on other sites
Quote:
Original post by DrEvil
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.


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

Share this post


Link to post
Share on other sites
What he meant is that you should never store an address to an element in a vector. In your case, you're outputting the pointer itself, so that's good, but if you were to output the address of this pointer (like if you stored objects instead of pointers), this new pointer could become invalid if after adding items to your vector it had to resize itself (by allocating a new pool of memory twice as big, and copying all of its content to the new pool). As you can see, after "moving" the items, their address change so that's why you can't rely on pointers to elements in a vector.

Hope this helps

(and I hope it was clear, I'm quite tired heh)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement