News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Modifiers in C++

Started by sukishan, Jul 10, 2009, 10:04 PM

Previous topic - Next topic

sukishan

Modifiers

In a declaration, modifiers before the type name apply to all objects in the list. Otherwise they apply to single objects.

  int* p, q;           // p is a pointer, q is an int
  const int a=0, b=0;  // a and b are both const


const

const objects cannot be modified once created. They must be initialized in the declaration. By convention, const objects are UPPERCASE when used globally or as parameters.

  const double PI=3.14159265359;  // Assignment to PI not allowed


References
A reference creates an alias for an object that already exists. It must be initialized. A reference to a const object must also be const.
  int i=3;
  int& r=i;         // r is an alias for i
  r=4;              // i=4;
  double& pi=PI;    // Error, would allow PI to be modified
  const double& pi=PI;  // OK
A good beginning makes a good ending