What’s the closest thing in C++ to retroactively defining a superclass of a defined class?

You can do the following: class C { struct Interface { virtual void bar() = 0; virtual ~Interface(){} }; template <class T> struct Interfacer : Interface { T t; Interfacer(T t):t(t){} void bar() { t.bar(); } }; std::unique_ptr<Interface> interface; public: template <class T> C(const T & t): interface(new Interfacer<T>(t)){} void bar() { interface->bar(); } }; … Read more

Why implicitly check for emptiness in Python? [closed]

This best practice is not without reason. When testing if object: you are basically calling the objects __bool__ method, which can be overridden and implemented according to object behavior. For example, the __bool__ method on collections (__nonzero__ in Python 2) will return a boolean value based on whether the collection is empty or not. (Reference: … Read more

Proper way to dynamically add functions to ES6 classes

Your solution is fine, though it’ll be better to create all those methods once on a prototype level: [‘GET’, ‘PUT’, ‘POST’, ‘DEL’].forEach((method) => { Executor.prototype[method] = function (body) { return this.request(method, body) } }) prototype approach is slightly faster, because this code is executed only once, while constructor code is executed every time new instance … Read more

Hashes of Hashes Idiom in Ruby?

You can pass the Hash.new function a block that is executed to yield a default value in case the queried value doesn’t exist yet: h = Hash.new { |h, k| h[k] = Hash.new } Of course, this can be done recursively. There’s an article explaining the details. For the sake of completeness, here’s the solution from … Read more

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