Our company founded in 2010 to create commercial application based on customer needs.

These developments contributed own libraries and tools, the first of which a serialization micro framework is released.

We also add new articles in every week with code examples, tutorials which could helps software developers work, and promote better applications to deploy.

title link

C++ serialization tool - simply parse your current declarations and generate code within a few click for complex data structures serialization (supports: fundamental types, STL containers and tuple, smart pointers)

Click here to learn more.

Articles, code examples, tutorials Articles


Company introduction - software developments
Main entity of page: 
C++, example, Serialize, tutorial
Date modified: 
Sunday, January 29, 2017 - 23:30
Date published: 
Mega-Siraj Bt

Good cop/bad cop interview - 2nd phase

Welcome back,

If you may miss the first part, you can read it Good cop/bad cop interview - start.

Good cop - I'm sure you don't have that C++ labelled bullet in your leg! These codes were tricky but not for you!

Bad cop - Yes, because you do not figure it out without a compiler!

Good cop - That not true. I knew those.

Bad cop - Liar.

Good cop - Don't start to piss me of again Bad cop!

Bad cop - Stop lying!

After all, here we again. Our brave admissions are fight again, unsure the job applicant, discredit the company! This is the time to stop them and get back to some more tricky examples!

[code]#include <iostream>

int main()
unsigned c = 2004;
const int &d = c;
std::cout << "c value: " << c << std::endl;

c = 2012;

std::cout << "c value: " << c << std::endl;
std::cout << "d value: " << d << std::endl;

Bad cop - Ahm... the output?

The evil within is the unsigned int => ref int. This is only works when we declare our integer reference as const. This let happen an implicit conversion in the background from unsigned integer to signed integer. For this step there is a hidden memory allocation to hold the converted value of c for the reference of d. Passing method parameters as const references may do this trick (implicit conversion) behind.

Bad cop - But what we really love in C++ is the following: (Bad cop show some empathy and enthusiasm, but be sensible!)

[code]#include <iostream>

class Base
Base() {std::cout << "Base()\n";}
~Base() {std::cout << "~Base()\n";}
Base(const Base&) {std::cout << "CopyBase\n";}

class Derived : public Base
Derived() {std::cout << "Derived()\n";}
~Derived() {std::cout << "~Derived()\n";}
Derived(const Derived& other) {std::cout << "CopyDerived\n";}

Base* create(Base g)
return new Derived();

int main()
Derived d;

Base* g = d.create(Base());
delete g;

return 0;

Bad cop - what about an output?

Good cop - yes that would be nice!

(Thanks God, they agree on something for the first time! :)

This example tries to figure out that the applicant knows how virtual destructor works. (Or at least knows what could happen when it's missing?!) This example is good as the parameter static and dynamic type is clear. We have a variable d, which type is Derived. This variable created on the stack and freed when we run out of its scope. The ctor and dtor call is obvious - Base() -> Derived() when destroyed -> ~Derived() -> ~Base().

But we create a g variable where the static type is Base, but the dynamic type is Derived! That's okay, but when the destructor called by the delete, then only ~Base() gets called, because it's destructor is not virtual! There is no virtual table, so there's no chance to know where to go when destructing g.

(While our brave cops do not check this code with a compiler, they forget to ask about the Derived class copy construct missing explicit Base class initialization. ;)


We may continue... (With our experiences)

Good cop/bad cop interview - begin


Good cop - This article is about how good is C++!

Bad cop - Yeah! Because you do not understand it!

Good cop - Ahm... let see some really exciting examples of C++!

Bad cop - or, maybe, now we can see how miserable you are!

Good cop - What? No, no. This could be very useful code examples, which helps your everyday life!

Bad cop - or, you can feel yourself smarter than the job applicant! What a wonderful character you are! (no you are miserable)

Good cop - What? R U F4cking with me? I'll show you who is bad!

Bad cop - WAIT! That should be me?!?!?! Or????

We (I and one of my college) have interviewing job applicants for C++ positions. We mostly trying to plays the good/bad cop situation, but honestly, we mostly playing both on the good side (how cowards we are  :).

I will explain my personal experience about how it goes as a job applicant later, but first let's give a session to "Bad cop"!

Bad cop - Hi! We really glad to have you here! Our method of HR technic is a personal interview! So, let's just go through some simple C++ code examples! I'm sure with your experience you will easily figure out the answers!

Bad cop - Something really simple:

[code]#include <iostream>

void modify(int* number)
int b = 20;
number = &b;

int main(int argc, char* argv[])
int a = 10;
int* n = &a;

std::cout << *n << std::endl;

return 0;

Bad cop - what is the output? (Any other problems?)

  • Crash? Undefined behaviour? 5? 10?

Of course it will output 10. This code is a bit tricky, but clarify a simple step. Function parameters are copied (except reference). In this case within the scope of "void modify(int* number)" the integer pointer is copied. The parameter number is an integer pointer which's points to the same memory location as v, as long as we overwrite it at line 6. Bad cop likes when the job application wants to brake the hand of that person who writes this code. (This behaviour is not exclusion from the interview process ;)

Bad cop - I know you pass this! Let's see the next one:

[code]#include <iostream>

class MyClass
void foo()
std::cout << "This class is MyClass" << std::endl;
int id;

class Derived : public MyClass
void foo()
std::cout << "This is class Derived" << std::endl;

int main(int argc, char* argv[])
MyClass* my = nullptr;



return 0;

Bad cop - and the Question IS, what is tha f****ng output???? :D (Bad cop likes to raise the stress level a bit)

Bad cop - so, crash? Undefined behaviour? Maybe 10? (He is a real jerk sometimes, excuse us ;)

Calling a method on a nullptr? What happened under the hood?

As we know the memory layout, the MyClass implementation goes to the code segment. In this case, there aren't any use of tag members within void foo(), so there aren't any object call. There aren't any polymorphism, so there aren't any call to the virtual table, because it won't needed to exist.

And line 29? I hope you strive to break some hands caused by the C cast! This situation much more shitty, because it will be a static_cast<Derived*>(my), so after all we get that bullet in our leg, while it will be a call to Derived::foo() after all!


More bad cop and weird code examples for job applicants in the next article!

Range-based loop, STL algorithms


When range-loop came into picture, I've started using it as soon as I could. As a programmer I'm looking for the simplest solution, which covers the problem, do the test and deploy. Let then the Computer do the dirty work! I'm its master! ;)

A person always looking for a better solution. A programmer is looking for a more simpler one, because over time, the code will be a subject of a rewrite or refactor (I still hear the screaming of managers, caused by these words ;)

Next is a source code for a range-loop method and an STL algorithm. They do the same thing, but looks a bit different. (We can also look at the code of those STL templates, and we'll find those for's ;)

This is a simple example, which not based on any real life story:

[code]#include <QString>

struct MyStruct
MyStruct(int id, QString text)
    : _id(id)
    , _text(text)

int _id;
QString _text;

At this point we have a struct declaration, which will plays as a key-value container on later. (Why is it key-value container then? that would be too simple ;)

Time to use our declaration, and set up some goals:

[code]#include <QList>

int main(int argc, char *argv[])
    // key-value pairs
    QList<MyStruct> _myNumbers = {MyStruct("0", "null"),};
    // request
    QList<int> _requests = {3, 5, 8};

    // result
    QList<QString> _result;

    // this is the place of solution code
    return 0;

The task is the following. Lookup each number from _requests within _myNumbers, and append a copy of the corresponding MyStruct's _text value to _results. (You are right! this job do not need a computer! this can be solved on a squared paper. believe me, there are simpler things are in this world which already programmed by big-BIG companies. :)

I'll first create a range-loop solution:

[code]for (auto& item : _requests)
    for (auto& elem : _myNumbers)
        if (elem._id == item)

Next the same goal, but simpler! :D

[code]_result = std::accumulate(_requests.begin(), _requests.end(), QList<QString>{}, [this](auto& a, int item){
    auto it = std::find_if(_myNumbers.begin(), _myNumbers.end(), [item](auto& elem){
        return elem.id == item;

    if (it == this->_detail.end())
        a.append(QString("No number like this one: %1").arg(item));

    return a;

Most of the range-loops could be replaced by with STL algorithms. Why is it good for? (for? I mean why is it good to use STL? ;)


  • The code will be more clear to others. The STL algorithms operations are obvious. Any container manipulation could be feasible.
  • Simpler to maintain. As we check this code parts, we surely know what the original author wants to do. Many for-loops could not be simple to understand
  • Easier bug fix. We could trust in STL code, which helps us reduce the error opportunities. (I know, we don't trust in STL at all! Just in ourselves! :)


I hope this article helps, and leads to refactor! (oh nooo, those screaming voices again... :)