Virtual Methods or Function Pointers

Approach 1 (Virtual Function) “+” The “correct way to do it in C++ “-” A new class must be created per callback “-” Performance-wise an additional dereference through VF-Table compared to Function Pointer. Two indirect references compared to Functor solution. Approach 2 (Class with Function Pointer) “+” Can wrap a C-style function for C++ Callback … Read more

overriding protected internal with protected!

We cannot modify the access modifiers when overriding a virtual method in derived class. That statement is false. You can and must change the access modifiers when in precisely the situation you describe. In other situations you must not change the access modifiers. I refer you to section 10.6.4 of the specification, which states: an … Read more

Why is a call to a shadowing non-virtual member function in the derived class not calling the base class member function?

Yep, you are misunderstanding a little. The method of the same name on the derived class will hide the parent method in this case. You would imagine that if this weren’t the case, trying to create a method with the same name as a base class non-virtual method should throw an error. It is allowed … Read more

Why is a call to a virtual member function in the constructor a non-virtual call?

Calling virtual functions from a constructor or destructor is dangerous and should be avoided whenever possible. All C++ implementations should call the version of the function defined at the level of the hierarchy in the current constructor and no further. The C++ FAQ Lite covers this in section 23.7 in pretty good detail. I suggest … Read more

What are the differences between overriding virtual functions and hiding non-virtual functions?

What is function hiding? … is a form of name hiding. A simple example: void foo(int); namespace X { void foo(); void bar() { foo(42); // will not find `::foo` // because `X::foo` hides it } } This also applies to the name lookup in a base class: class Base { public: void foo(int); }; … Read more

Return Type Covariance with Smart Pointers

You can’t do it directly, but there are a couple of ways to simulate it, with the help of the Non-Virtual Interface idiom. Use covariance on raw pointers, and then wrap them struct Base { private: virtual Base* doClone() const { … } public: shared_ptr<Base> Clone() const { return shared_ptr<Base>(doClone()); } virtual ~Base(){} }; struct … Read more

Performance of Expression.Compile vs Lambda, direct vs virtual calls

I didn’t find any answer, so here is the performance test: using System; using System.Diagnostics; using System.Linq.Expressions; using System.Reflection; using System.Reflection.Emit; namespace ExpressionTest { public interface IFoo { int Bar(); } public sealed class FooImpl : IFoo { public int Bar() { return 0; } } class Program { static void Main(string[] args) { var … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)