Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. const pointer

const pointer

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    act_x
    wrote on last edited by
    #1

    I need to refer something on C++ and found this #include using namespace std; int main() { int foo = 4; int bar = 16; // ptr - non-constant pointer, non-constant data // int* ptr = &foo; *ptr = 6; // OK: Data is non-constant, so it can be // changed via ptr ptr = &bar; // The pointer is non-constant, so it may *ptr = 22; // point to other data And change it // ptr_to_const - non-constant pointer, constant data // const int* ptr_to_const = &foo; return 0; } When i read this const int* ptr_to_const = &foo; Non constant pointer to constant data ; But foo isnt declared as constant . Why does the compiler not warn me of this ?

    P D H 3 Replies Last reply
    0
    • A act_x

      I need to refer something on C++ and found this #include using namespace std; int main() { int foo = 4; int bar = 16; // ptr - non-constant pointer, non-constant data // int* ptr = &foo; *ptr = 6; // OK: Data is non-constant, so it can be // changed via ptr ptr = &bar; // The pointer is non-constant, so it may *ptr = 22; // point to other data And change it // ptr_to_const - non-constant pointer, constant data // const int* ptr_to_const = &foo; return 0; } When i read this const int* ptr_to_const = &foo; Non constant pointer to constant data ; But foo isnt declared as constant . Why does the compiler not warn me of this ?

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      Read this http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/html/_pluslang_const_and_volatile_pointers.asp[^]


      -prakash

      1 Reply Last reply
      0
      • A act_x

        I need to refer something on C++ and found this #include using namespace std; int main() { int foo = 4; int bar = 16; // ptr - non-constant pointer, non-constant data // int* ptr = &foo; *ptr = 6; // OK: Data is non-constant, so it can be // changed via ptr ptr = &bar; // The pointer is non-constant, so it may *ptr = 22; // point to other data And change it // ptr_to_const - non-constant pointer, constant data // const int* ptr_to_const = &foo; return 0; } When i read this const int* ptr_to_const = &foo; Non constant pointer to constant data ; But foo isnt declared as constant . Why does the compiler not warn me of this ?

        D Offline
        D Offline
        Dennis Gourjii
        wrote on last edited by
        #3

        A pointer to constant data needs to be initialized, that's what this line does, and no specification says the "initializer" must be constant. Using pointers to constant data is in essence declaring an intention not to modify that data, nothing more. Sure, if you try to modify it, no warning will be shown - it'll be an Error! The other possible case is declaring a constant pointer. You certainly can initialize it, but not make it point anywhere else afterwards.

        1 Reply Last reply
        0
        • A act_x

          I need to refer something on C++ and found this #include using namespace std; int main() { int foo = 4; int bar = 16; // ptr - non-constant pointer, non-constant data // int* ptr = &foo; *ptr = 6; // OK: Data is non-constant, so it can be // changed via ptr ptr = &bar; // The pointer is non-constant, so it may *ptr = 22; // point to other data And change it // ptr_to_const - non-constant pointer, constant data // const int* ptr_to_const = &foo; return 0; } When i read this const int* ptr_to_const = &foo; Non constant pointer to constant data ; But foo isnt declared as constant . Why does the compiler not warn me of this ?

          H Offline
          H Offline
          Henry miller
          wrote on last edited by
          #4

          Because prt_to_const is a promise that you won't modify the data through ptr_to_const, not that the data it points to cannot change by other means! One good reason to use const is to force other code to not change something. A class might return a ptr_to_const, that points to private internal class data. The class itself might modify the data via some function call latter, but it would be a mistake to let anything other than the class modify that internal data.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups