More Interesting C++ Notions

I’ve been doing some work in C++ again the past few days, and it is always good to check back on your first computer language even though these days I prefer to be called a Lisper and use Python a lot more. This time, I was checking out the source of the utterly mind-blowing and awesome OpenCV project! While looking at the code for their Support Vector Machine class in the ML module, I noticed 2 interesting ideas and after some background research, here they are:

1. Const Functions

We all know very well about the const keyword in C/C++ and multiple other languages. This keyword helps define constant variables so that we can be assured that our code does not change the value of PI from 3.1415 to 2.71828 (hi-five if you got the hint!). So what exactly are const functions? Functions that don’t change their behavior? Pfft, of course not silly! Functions never change their behavior unless you edited them or your code got hacked because you allowed some form of code injection (I place my bets on stack smashing). In this case, the issue has to do with C++ and its object system. Now when we define an object in C++, we can mark it const to ensure that the object reference of a variable and the object itself is enduring and unchanging. But what is the purpose of methods in a class? They only exist to change the state of an object, which shouldn’t happen if we have marked an object as const. So what we do, is that we mark the functions that don’t change object state as const (similar to static functions in other languages, remember C++ only allows static for variables) so that the compiler knows that it is safe to call this function/method from a const object. Of course, you can always call a const function from a non-const object since this function is guaranteed to not change the invoking object’s state which is alright for a non-const object. The rules can be summarized as:

  1. Const functions can always be called.
  2. Non-const functions can only be called by non-const objects.

2. Typedef for Functions

Admit it, we abuse the typedef keyword. What was added as syntactic sugar to help code readability and shorten elaborate declarations by allowing aliases for keywords is overused by competitive programmers and those without a fundamental sense of code clarity. Hey, I’m not innocent myself, using typedef for an advantage in programming competitions, but the real aesthetic of this keyword is how we use it to improve our code’s accessibility.

Philosophy aside, we have been content to use typedef just to alias our variable declarations, but little do people know that typedef can apply to functions as well.

Take this code for example:

As you can see, the main idea here is that we can define an alias for the Addition function and call it as Add. To typedef a function, we need to declare the alias as a function pointer after the typedef keyword while specifying the parameters and return type as in a normal function signature (line 10). Then all we need to do is map the function pointer to a variable (line 26) and use that variable as a normal function (line 28). Voila! Now you can define easy to remember and map function names for those pesky, over-complicated functions whose names make no sense without touching (or worse, breaking) the original code!

Hope you liked these unique ideas present in C++, since the language receives enough flak for being bulky (something that I am reading upon in a research paper by the legendary John Backus). You can read about my earlier C++ notions here.

Eviva!

Advertisements

2 thoughts on “More Interesting C++ Notions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s