For me const is/was hard to understand

Hey there,

This post is just to share my pain understanding the const and tipping the creators/ teachers that this might be a harder concept for some to understand than it might seem. Also if you are struggling with this at first: don’t worry you are not alone. There are at least two of us. :wink:

I come from the perspective or relatively inexperienced coder and developer, and my terminology might be lacking, but I try to avoid confusion.

I feel a bit silly writing this because const seems so simple and the code is easy to replicate, but at the same time I had to watch the video, or parts of it, several times, and read through some of the discussion before I think I understood what we are talking about and what it might be useful for.

The big problem for me was that I really like using variables and strings and making a function unable to edit them seems counter-intuitive to me. Also, the ramifications of using const were/are a bit hard to grasp for me such as the scope the part of the code that we are locking down/ limiting with the command.

Anyways I think that even if I was totally unable to take on the challenge, I kinda understand the concept now, and will learn more about through trial and error and using it.

The simplest way I could describe my understanding at the moment is:

const marks/ locks a part of the code so that it may not modify any variables or strings and such, but it can still handle them to pass them on for example. It is used to mark a section of code for yourself and others to understand the nature of the function better/faster/easier, and to limit the human errors produced by coders editing code. Using it also helps to later on track down issues, because you may be able to skip the part protected by const.

Side note: On the other hand it also could be source of an issue, right? I guess it comes down to understanding what kind of issues one is facing.

Your definition for ‘const’ is true but i have to mention that it only works when you use it on method’s(functions) inside a ‘class’.
It cannot be the source of an issue because the thing is when you are making a method for your class you must be sure that the method your making is responsible for changing any >> inside class << variable’s or not for example: GetVariable() SetVareable() or Move() Reset() and…, we always use const on Get*() methods because their job is getting the value not setting it!

1 Like

Thanks for the clarification. It indeed adds to my definition and understanding.

Now I’m yet one more step closer to understanding C++. ^^

1 Like

Imagine a strip mall. Shops have windows, and you can see through the windows to see what they’re selling. You, standing outside the store, can’t change anything about what they’re selling, but you can still see it. That window is a const function returning some variables, but unable to change the variables. Some other part of the class/store might change things (like the current inventory), but it isn’t changing just from you looking through the window.

1 Like

Privacy & Terms