Difference between revisions of "Const"

From Chilipedia
Jump to: navigation, search
(Variables)
Line 88: Line 88:
 
* [https://stackoverflow.com/questions/136880/sell-me-on-const-correctness Stack Overflow page describing the benefits of const-correctness]
 
* [https://stackoverflow.com/questions/136880/sell-me-on-const-correctness Stack Overflow page describing the benefits of const-correctness]
 
* [https://isocpp.org/wiki/faq/const-correctness Definitive FAQ article on const-correctness]
 
* [https://isocpp.org/wiki/faq/const-correctness Definitive FAQ article on const-correctness]
 +
 +
== See Also ==
 +
* [[Beginner C++ Game Programming Series#Topics Covered|<code>List of topic covered in Beginner C++</code>]]

Revision as of 22:52, 11 September 2017

const is a keyword that is applied to variables, references, and functions. It's basically like entering into a contract with the compiler that says "Shit's not gonna change yo!". A lot of scrub-level devs overlook this feature, but it is the shit and will make your code almost as rock SOLID as Chili's Johnson.

Why const?

You often have variables where you store the result of some calculation, for example, but you don't ever have to modify the variable during its lifetime. Mark that shit as const! That way, the compiler will tell you if you fuck up and accidentally write some code that would modify the variable. Take this code:

void foo( const int defcon )
{
   if( defcon = 1 )     //< FLAGGED AS COMPILER ERROR! WORLD SAVED!
   {
       fire_missiles();
   }
}

Without the const, the operation would store the value 1 in the variable defcon, then the value of defcon would convert to the boolean value true, and the world would be fucked! This is just a simple example; proper use of const can solidify your code in many ways. This is called 'const-correctness'.

Use Cases

There are a number of different situations where you can use const, and the meaning of the keyword changes somewhat depending on the situation.

Variables

You can declare a variable as const with the following syntax.

const int x = 69;

When a variable is declared in this manner, operations that modify the variable will cause a compiler error. It will also be impossible to pass a pointer or reference of that variable, unless the pointer or reference is also const. Note that you must assign a const variable a value upon creation, since it will not be possible to assign one afterwards.

References

You can declare a const reference as follows:

const int& x_ref = x;

This is especially useful in function declarations, where we want to pass an object by reference (we don't wanna copy the whole damn thing!), but we aren't going to be modifying it:

void DoSomeShit( const MyObjectType& obj );

Declaring a reference parameter like this prevents code in the function body from modifying the object, and it also enables us to pass const objects to this function (if the reference were not const, we would not be allowed to pass in const objects).

Pointers

Much like references, you can declare const pointers that are not allowed to modify their targets.

const int* px = &x;
void DoSomeShit( const MyObjectType* obj );

We sometimes call these 'pointer-to-const' because it is the object that they point to that is not modifiable. There is another kind of const pointer, defined like this:

int* const px = &x;

This kind of const pointer is not retargettable. So here it is the pointer itself that is being locked down. You can also have both!

const int* const px = &x;

This pointer cannot be retargetted, and it cannot modify its target.

Member Functions

Member functions can also be declared as const. You might think that makes no goddamn sense, a function doesn't store anything--it can't be modified! But here the meaning is a bit different.

void MyMemberFunc() const;

When you make a member function const, what you're saying is 'this function will not modify the object on which it is called'. This gives you two benefits: first of all, the compiler will tell you if you unintentionally do something that could modify the object inside of the function, and secondly, it allows you to call that member function even on objects that are const!

class MyClass
{
public:
	void MyConstFunc() const;
	void MyFunc();
};

MyClass myObj;
myObj.MyFunc(); // fine
myObj.MyConstFunc(); // fine

const MyClass myObj_const;
myObj_const.MyFunc(); // GET FUKT M8!!
myObj_const.MyConstFunc(); // fine, const func called on const obj

const MyClass& ref = myObj;
ref.MyFunc(); // GET FUKT M8!!
ref.MyConstFunc(); // fine, const func called on const ref to obj

Videos

External References

See Also