C++ Constants

A constant is a variable type which cannot be changed during run-time of a program. Once it has a set value, it stays that way until you change your code. There isn't much to constants, but they can cause errors that aren't explained well in your IDE.

toc_collapse=0;
Contents 

Declaration

To declare a constant all you do is put const before the data type, like this:
const int intNUM = 5; // makes intNUM a integer with a constant value.
Also be sure to remember to set the variable to something within the declaration so it can have a value.

There is also another way to declare constants, these are called definitions (the compiler takes the definition and literally replaces all instances of the definition in your code with the constant). Here's an example:

#define hi "hello" // making hi a constant with the value hello.
#define pie 3.14

#define is helpful because it doesn't take up as much memory as variables.

Compiling Errors

Now, what happens when you're trying to edit a constant value? In Dev-C++ (the IDE I use), it never really tells you that you're try to edit a constant value. For example, this code:

const string magic = "World"; // sets magic as constant string
cin >> magic; // tries to set the constant 'magic' to what the user types

produces this error:
no match for 'operator>>' in 'std::cin >> magic'
It will not tell you exactly that you are trying to edit a constant value, but it will tell you something is wrong with that part of the code. Also note that that is a compile error, so you will not even be able to compile when trying to edit a constant value (in Dev-C++).

Statics

Statics are not the same as constants, they can change. You declare a static by typing static before the data type or function.

There are a couple good things about statics. The first is that statics are allocated when the program starts, and deallocated when the program ends. Another upside to statics is that when declared, they're declared with the value of 0 (Unless another value is set).

You must set the class static variable inside the file to a value, otherwise it won't work.

Inside a function, a static can retain its current value. Here's an example of a static variable inside a class and a function with a static variable inside of it.

#include <iostream>
using namespace std;

class Counter {
 public:
  static int m_count;
};

int Counter::m_count = 0;

void IncrementDifferentCounter(){
  static int different_counter = 0;
  cout << different_counter++ << endl;
}

int main(){
  cout << Counter::m_count << endl; // outputs 0
  cout << ++Counter::m_count << endl; // outputs 1
  Counter::m_count = 5;
  cout << Counter::m_count << endl; // outputs 5
  IncrementDifferentCounter(); // outputs 0
  IncrementDifferentCounter(); // 1
  IncrementDifferentCounter(); // 2
  IncrementDifferentCounter(); // 3
  IncrementDifferentCounter(); // 4
}

Omee's picture

I never saw the reasons why

I never saw the reasons why to use constants, it always seemed sort of useless to me, but i guess it saves memory.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.