|
-
November 14th, 2009, 07:26 PM
#1
const pointers
Hello!
I have a question about constant pointers. I am aware that with regular pointers, you can access the memory addresses next to a certain memory address to handle arrays/vectors, etc. such as in this example:
PHP Code:
#include <iostream>
void main() {
vector<SomeClass_t> objects (10);
SomeClass_t* baseobject (&objects[0]);
std::cout << (baseobject + 2)->somefunc(); //would reference objects[2]
}
However, I have a function to which I am passing such a base object. I want the base object to be constant so that I can always keep track of where the start of the array is. It appears though that accessing memory offset from a constant pointer cannot be done in the same way, as in this example:
PHP Code:
#include <iostream>
void main() {
vector<SomeClass_t> objects (10);
const SomeClass_t* BASEOBJECT (&objects[0]);
std::cout << (BASEOBJECT + 2)->somefunc(); //error
}
This would return the error " Cannot convert from 'const SomeClass_t' to 'SomeClass_t&' "
Is there a special way of handling this, or should I be looking for a different way to handle this scenario?
Thanks in advance!
Last edited by KezRst; November 14th, 2009 at 08:18 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|