Why I love the way C++ sucks
Foreword: This is my first "blog post"-thing. I hope it's half as interesting as it was fun for me to write it.
After you read the post, you can use the 'help' command in the console at the bottom of the page to access the rest of the website.
Part 1: Of languages and taste
Youtube has recently started recommending me some "programming/it" content, and it's been interesting. One of the more personally interesting things I noticed, was the bad reputation of C++.
In the back of my head I have known for a long time that C++ is considered "bad" and/or "confusing" by big segments of the developer community, but this never coincided with my own expericence.Part 2: An apparent 'Foot-Gun'
It was with this weird feeling in the back of my head for a few days, when writing some personal C++ code, I had a bit of an 'epiphany'. I thought I had found something to reconcile my love for C++, and everyone else's distaste for it.
The code in question (distilled as a standalone example) goes as such:
#include <iostream> class Parent { public: virtual int test() { return 0; } }; //'Child' inherits from 'Parent' (duh) class Child : public Parent { public: int test() override { return 1; } }; void print(Parent& p) { p = Parent(); //For the uninitiated, 'std::cout' prints to standard output std::cout << "Val: " << p.test() << std::endl; } int main(int argc, char** argv) { Child c; print(c); return 0; }
I showed this example to a few of my friends/colleagues, and my dad (a now retired programmer, from the 'good old days') for good measure. None of them C++ experts, most of them got it wrong. If you feel like it, take your time to think about what this code will output.
I'll wait. Here's the output of the above program:Val: 1
Part 3: What's in an '='
So what's going on here?
If you're like my friends or my dad, you are confused at the p = Parent(); line. Aren't we assigning a newly created 'Parent' to 'p'?
Yes, but mostly no. From cppreference.com, "A copy assignment operator [...] copies the content of the argument [...]" (emphasis mine).
So what we're actually 'assigning' is a copy of the content of the newly created 'Parent' into 'p', but the type of 'p' itself doesn't change, therefore when it comes to resolving a virtual method (a method that can be overridden by child classes), the Child's 'test()' method is called.
This would be different if we were dealing with pointers (audience gasp!), which is what most higher level languages use under the hood for 'objects'. In that case, we would *actually* be swapping objects, type and all, instead of just content.Part 4: I understand, but WHY?!?
Well, because C++ is AWESOME (IMO). There are cases, be it encapsualtion, performance or just readability of code, where changing the type of an object is undesirable, but copying the object's contents makes sense.
In C++ we can also overload this 'copy assignment operator', to give it exactly the meaning that we want. Imagine for example a class that holds some reference to an external system.