News:

Build a stunning handcrafted website with IT Acumens

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

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Name:
Email:
Verification:
Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview