CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: const pointers

Threaded View

  1. #1
    Join Date
    Sep 2009
    Posts
    27

    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_tobjects (10);
      
    SomeClass_tbaseobject (&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_tobjects (10);
      const 
    SomeClass_tBASEOBJECT (&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
  •  





Click Here to Expand Forum to Full Width

Featured