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

Thread: const pointers

  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.

  2. #2
    Join Date
    Sep 2009
    Posts
    27

    Re: const pointers

    I think I have found my solution to this problem. Finding the solution though brought up a lot more questions. After some more searching I found there is a difference between 'pointer to a const variable' and 'const pointer'

    I have this code now:

    #include <iostream>

    PHP Code:
    void main() { 
      
    vector<SomeClass_tobjects (10); 
      
    SomeClass_t* const BASEOBJECT (&objects[0]); 
      
    std::cout << (BASEOBJECT 2)->somefunc(); //error 

    This works as I would expect it to. I am having a hard time getting my head around the difference between the two though...(correct me if I am wrong here)

    SomeClass_t* const BASEOBJECT (&objects[0]);

    This is a declaration of a pointer whos address is constant. Because the 'const' keyword comes directly before BASEOBJECT, it means BASEOBJECT is constant, making the adress it holds read-only. So it would serve as a read/write non-movable pointer.

    const SomeClass_t* BASEOBJECT (&objects[0]);

    This is a declaration of a pointer who's address has a value which is constant. Whether or not the value itself is constant or not is irrelevant, because when accessed through the pointer, the pointer will treat it as const. So it would serve as a read-only movable pointer.



    Just trying to wrap my head around this, any clarification would be appreciated.
    Last edited by KezRst; November 14th, 2009 at 08:19 PM.

  3. #3
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Re: const pointers

    Think of it this way...

    const always refers to the "thing" just before it.

    Someclass_t const* says you have a pointer to a const Someclass_t object.

    Someclass_t* const says you have a const pointer to a Someclass_t object.

    const Someclass_t* is exactly the same as the first example.

    The third example is the exception when thinking about it as "the thing just before it", because there is nothing before it. However, this is a convention that is allowed by the C++ standard, and seems to be more commonly used than the first example.

    You also have the following...

    const Someclass_t* const which is a const pointer to a const Someclass_t object.

    Someclass_t const* const which is also a const pointer to a const Someclass_t object.

    Hope this helps.

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: const pointers

    Quote Originally Posted by KezRst View Post
    However, I have a function to which I am passing such a base object.
    Are you passing a pointer because the function being called is expecting plain old arrays too.?
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

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