What will be the output of the code below?
#include <iostream>
#include <memory>
using namespace std;
namespace abyss {
class A {
public:
virtual ~A() {}
virtual void f() { cout << "A::f" << endl; } // base is virtual
};
class B: public A {
public:
virtual ~B() {}
void f() { cout << "B::f" << endl; } // this is also virtual
};
class C: public B {
public:
virtual ~C() {}
void f() { cout << "C::f" << endl; } // continues to be virtual
};
}
int main() {
auto_ptr<abyss::A> base(new abyss::C);
base->f();
// method called using pointer to base gets dispatched to C::F
return 0;
}
Take notice of the “once virtual always virtual” rule that states that once a method is qualified as virtual there is no way to turn off the dispatching mechanism provided by the language for virtual functions. Unlike Java, C++ does not have the notion of final (which interrupts the dispatching). This means that, even though neither B or C classes qualify f() as virtual, the call to base->f() shall be dispatched as if it was.
I like to follow the convention to place an explicit virtual qualifier to make clear the expected behaviour. It would be nice to have some kind of warning from the compiler, but no issue is raised (at least when using g++ 4.5.1).
From time-to-time the question re-emerges “How to round floating values?”. (I know its stupid, but after this post, I’m sure the mental note will stick!)
Yes, there is a round function in C and C++ standards.
It was introduced in the C99 standard, and it is also available in the C++98 standard. The round function is defined amongst others mathematical related function in math.h (cmath for C++ purposes).
But, for purposes not worth mentioning, I was using a pre-round compiler and had to roll my own function. The standard defines that the round function shall return the nearest integer value in floating point format, rounding halfway cases away from zero. This means that negative values rounds up in absolute value (i.e. -1.7 rounds to -2.0, while -1.2 rounds to 1.0). Here is the result of my experiments (abyss is the namespace that stores my experiments, as in “It is by going down into the abyss that we recover the treasures of life.“).
#include <cassert>
#include <iostream>
#include <cmath>
typedef double(*round_function_t)(double);
namespace abyss {
double round(double x) {
return (x > 0.0) ? std::floor(x + 0.5) : std::ceil(x - 0.5);
}
}
void testRoundedValue(double value, double expected,
round_function_t round_function) {
double rounded = round_function(value);
std::cout << value << " rounds to " << rounded
<< " obtains " << expected << std::endl;
assert( rounded == expected );
}
int main(void) {
round_function_t round_func = abyss::round;
// use std::round for standard round
testRoundedValue(-1.70, -2.0, round_func);
testRoundedValue(-1.51, -2.0, round_func);
testRoundedValue(-1.50, -2.0, round_func);
testRoundedValue(-1.49, -1.0, round_func);
testRoundedValue(-1.20, -1.0, round_func);
testRoundedValue(-0.50, -1.0, round_func);
testRoundedValue(-0.49, -0.0, round_func);
testRoundedValue( 0.00, 0.0, round_func);
testRoundedValue( 0.49, 0.0, round_func);
testRoundedValue( 0.50, 1.0, round_func);
testRoundedValue( 1.20, 1.0, round_func);
testRoundedValue( 1.49, 1.0, round_func);
testRoundedValue( 1.50, 2.0, round_func);
testRoundedValue( 1.51, 2.0, round_func);
testRoundedValue( 1.70, 2.0, round_func);
return 0;
}

This huge coin has been following me all day. It’s the third blog were I find it today.
What do you say, head or tails?
Some (not to say most) of my days start at a small coffee shop that is visited on the way to the office. The day starts right after taking the usual latte and rice muffin, just before taking the usual elevator ride to the office on the 10th floor. It usually takes less than 10 minutes, but sometimes I ask myself why not work just right from the coffee shop.
So, what’s so important that makes it mandatory to be seated at my desk all day?
I guess, most of the time… well… nothing. Other times, everything.
… or, a “mental” note on how to do C++ Mixins using the Curiously Recurring Template Pattern [1].
A way to separate the dumb domain object (containing essentially data) from functionality needed for a specific use case purpose - based on DCI concepts presented in Lean Architecture [2] .
#include <iostream>
template <typename Derived>
class Mixin {
public:
virtual ~Mixin() {};
void functionA() {
dynamic_cast<Derived*>(this)->functionX();
std::cout << "do something specific!" << std::endl;
}
virtual void functionX() = 0;
};
class DumbObject : public Mixin<DumbObject> {
public:
void functionX() {
std::cout << "access dumb data!" << std::endl;
}
};
int main(void) {
DumbObject dumb;
dumb.functionA();
return 0;
}
References:
[1] Coplien, James O. “Curiously Recurring Template Patterns”. C++ Report: 24–27. 1995, February
[2] James O. Coplien, Gertrud Bjørnvig. “Lean Architecture: for Agile Software Development”. Wiley. August 17, 2010