Implementing the visitor pattern using C++ Templates

This can be done in C++11 using variadic templates. Continuing from Pete’s answer: // Visitor template declaration template<typename… Types> class Visitor; // specialization for single type template<typename T> class Visitor<T> { public: virtual void visit(T & visitable) = 0; }; // specialization for multiple types template<typename T, typename… Types> class Visitor<T, Types…> : public Visitor<Types…> … Read more

Why does a virtual function get hidden?

Assuming you intended B to derive from A: f(int) and f() are different signatures, hence different functions. You can override a virtual function with a function that has a compatible signature, which means either an identical signature, or one in which the return type is “more specific” (this is covariance). Otherwise, your derived class function … Read more

How do I use google mock in C?

I found a way to be able to mock bare C functions in google-mock. The solution is to declare foobar to be a weak alias that maps to foobarImpl. In production code you do not implement foobar() and for unit tests you provide an implementation that calls a static mock object. This solution is GCC … Read more

What happens if main() does not return an int value?

If main doesn’t return int, then you have an ill-formed program and behavior is undefined. Anything can happen. Your program might crash, or it might run as though nothing were wrong at all. Let’s suppose main returned something other than int, and your compiler and linker allowed the program to be made. The caller doesn’t … Read more