MohamedG's Log


Tiny C++ Unit Test Framework

I was working on a small project for personal use that I wrote in C++ and I wanted to test it. Since it’s a tiny project, I didn’t bother installing a well-known test framework that ships with a plethora of features. I just wanted a simple test runner that can show me a failure when… Read more

C++: Calling a Virtual Function From a Constructor is not Polymorphic

In C++, if you call a virtual function form a constructor, it won’t be polymorphic, meaning that the following code won’t behave as you may have expected: class Foo { public:     Foo() {         whoAmI();     }     virtual void whoAmI() {         cout << “Foo::whoAmI()” << endl;     } }; class Bar : public Foo { public:     Bar() {         whoAmI();     }     virtual void whoAmI() {        … Read more

Passing C++ Arrays by Value

Just in case you needed to, you can wrap an array into a struct/class and pass it by value to a function:template<typename T, int N>struct array {    T value[N];    T & operator[](int i) { return value[i]; }};template<typename T, int N>void passByValue(array<T, N> a) {    cout << “Value in function:” << endl;    for (int i =… Read more

Proxy Design Pattern

One of the useful design patterns is the proxy design pattern, it allows you to control access to an object via a proxy and also saves you the startup and cleanup overheads as you instantiate only what you use upon request (lazy initialization). Take a look at the following example: #include<iostream> #include<string>   using namespace… Read more