Thread: Can't convert from 'unsigned short ** ' to 'const unsigned short ** '? Why not?

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    Can't convert from 'unsigned short ** ' to 'const unsigned short ** '? Why not?

    Code:
    cannot convert parameter 2 from 'unsigned short ** ' to 'const unsigned short ** '
    Conversion loses qualifiers
    I've seen this error a few times and it kind of baffles me.
    I know you need a const_cast to remove constness, but if I'm reading this correctly, this error says it can't add constness.
    I've passed non-const parameters to functions that take const parameters more times than I can count without problems. Why would it complain about this only sometimes? I thought constness can always be implicitely added, but you have to explicitely remove it?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You're not adding constness. You're trying to convert a pointer to one type (unsigned short*) to a pointer to a different, unrelated type (const unsigned short *). Adding const is only allowed at the outermost level and an unbroken chain going inward. The reason for this restriction is that, if you could cast int** to const int**, you could subvert const-correctness without a cast:
    Code:
    const int ci = 0;
    int *pi; // Valid - uninitialized pointer to int
    int **ppi = π // Valid - ppi points at pi
    const int **ppci = ppi; // Valid? ppci now points at pi
    const int *pci = &ci; // Valid - pci is const and may point at the const int
    *ppci = pci; // Valid - *ppci is "const int *", which you can assign to
    // But uh, oh - pi points to ci now!
    *pi = 100; // Modify ci???
    
    // Since an unbroken chain is allowed, this is actually valid:
    const int *const *pcpci = ppi;
    *pcpci = π // Invalid. *pcpi is "const int *const" and cannot be assigned to.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    You're not adding constness. You're trying to convert a pointer to one type (unsigned short*) to a pointer to a different, unrelated type (const unsigned short *). Adding const is only allowed at the outermost level and an unbroken chain going inward. The reason for this restriction is that, if you could cast int** to const int**, you could subvert const-correctness without a cast:
    Code:
    const int ci = 0;
    int *pi; // Valid - uninitialized pointer to int
    int **ppi = π // Valid - ppi points at pi
    const int **ppci = ppi; // Valid? ppci now points at pi
    const int *pci = &ci; // Valid - pci is const and may point at the const int
    *ppci = pci; // Valid - *ppci is "const int *", which you can assign to
    // But uh, oh - pi points to ci now!
    *pi = 100; // Modify ci???
    
    // Since an unbroken chain is allowed, this is actually valid:
    const int *const *pcpci = ppi;
    *pcpci = π // Invalid. *pcpi is "const int *const" and cannot be assigned to.
    Ah, OK. I guess that makes sense. I'll probably forget this again later, but at least I know for now.
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Replies: 7
    Last Post: 02-08-2008, 06:31 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM