What’s worse than poking yourself in the eye with a fork? Learning to code in C++, that’s what! Hi, I’m Cliff. You’re probably here because you’re looking for info on how to write software programs that run on things like cell phones, TVs, robots, etc. I’m here to sell you an eye patch because of the eye-poking happiness I just endured these last few days. Seriously, I will sell you an eye patch for the low, low, price of $4.99! (Why do marketers and commercials always repeat the word “low” when trying to strike a deal? It’s as if the extra occurrence will trigger a spontaneous shift in perception and cause you to think, “Geez, this must be really deeply discounted!”) I will sell you the patch because you will need it. You will need the patch because you will poke yourself in the eyeball. You will poke yourself in the eyeball because it’s slightly less painful than what you’re about to learn. You’re about to learn this because you want to prove me wrong about my eye poking theory. You will be wrong. Then you will be wearing an eye patch.
Say you’re writing a C++ class. (Yes, say it… out loud for people to hear you. When they give strange glares simply explain that you are reading my whacky blog.) Now say you want to follow best practices and mark your class members as const to make them immutable because you’re all about that functional “no-side-effects” goodness. Now say you did this sub-consciously because the functional side of your mind likes to spontaneously assume control whenever you code, even when you are learning something simple. (By the way, are you still saying all of these things out loud? You can stop now because I’d like you to make it through the text before the authorities are called.) So one side of your mind has done this seemingly cool thing while the other side wasn’t thinking or looking. Now you’re in the middle of one of the class’ methods and attempting to do something non-trivial.
Because this is C++ and you’re not as familiar you try “the simplest thing that works”™. You’re having a bit of trouble because you want to now establish some sort of state in your class while the logic in the method rolls through a series of loops/steps to accomplish its task. You make a local variable of type std::string and everything seems to sorta compile. You now cut/paste the variable as a new member variable in the class header to hold the state. You are building a string using the variable as a buffer. All of a sudden the compiler complains with an error saying, “You are not dope enough to invoke the insert method that I was perfectly happy with moments ago. I hate you!” You try all different variations of the overloaded insert method with no success. Days roll by as you scratch a new bald spot into your scalp. (Bear in mind that normal people do not scratch their head that hard or that frequently but this error has you perplexed!) You eventually decide to read the error message text closer and realize that your compiler is actually complaining about a signature mismatch on the parameters! It is pointing to the first parameter referred to as “this” and crying because the “this” pointer is marked constant!
Looking in the header you doubly/triply confirm that your string variable is NOT marked “const” because that’s the only thing your brain can pick out of the error message. “This string is not const”, you scream at the display while re-reading the error text repeatedly. By this point office workers begin raising concerns. Co-workers refuse to invite you to lunch. Your 5 o’clock shadow reveals the amount of sleep you should have been getting nights prior. Then it hits you!
The first parameter of a member function in C++ is the object itself! The error is referring to the actual instance of object owning the method and somehow IT is marked const! How could this be??? At this point, (shortly after removing the fork from your eyeball) you see the identifier “const” prominently declared in the member function’s declaration! Because the member function is “const” the “this” reference is const! Because “this” is const it cannot mutate any member variables! Mystery solved and you are now off to Lens Crafters to schedule an optometrist appointment!
I have one eye left but I am going to make it through this C++ course I’m taking if it blinds me! (Yes, I’ve programmed in Braille before.) Did I mention how much I love writing in C++?