Constants

To put it simply, the const can essentially be thought of as a write protection of sorts.

 void voodoo(   const   Foo *   const   pInfo); 

In this example the contents of the data structure (first const) and the pointer to the structure (second const) cannot be altered . This function merely reads information contained within that structure and does not alter it or its pointer in any fashion. The const guarantees it! Of course that does not stop the programmer from casting the pointer to a const-less pointer and then modifying the data. But that would be bad!

The placement of the second const protecting the pointer is very specific, but the placement of the first const is a little more liberal . It can occur before or after the data type declaration such as in the following:

 void voodoo(   const   Foo       * const pInfo);  void voodoo(Foo   const   * const pInfo); 
Tip 

When working with C++ all data members private, protected or public can be protected with the inclusion of an appended const to the prototype and declaration.

 class foo { private:         int a;     public:         int b;        void voodoo(const byte * const pFoo)  const;  };         void foo::voodoo(const byte * const pFoo)  const  { //   *pFoo = 'a';   // The (first)  const  protects data! //     pFoo++;      // The (second)  const  protects pointer! //     a = b = 5;   // The  (third)  const  protects data members! } 


32.64-Bit 80X86 Assembly Language Architecture
32/64-Bit 80x86 Assembly Language Architecture
ISBN: 1598220020
EAN: 2147483647
Year: 2003
Pages: 191

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net