Access to static properties via this.constructor in typescript

but in typescript this.constructor.prop causes error “TS2339: Property ‘prop’ does not exist on type ‘Function’”. Typescript does not infer the type of constructor to be anything beyond Function (after all … the constructor might be a sub class). So use an assertion: class SomeClass { static prop = 123; method() { (this.constructor as typeof SomeClass).prop; … Read more

static vs extern “C”/”C++”

Yes, you are just lucky 🙂 The extern “C” is one language linkage for the C language that every C++ compiler has to support, beside extern “C++” which is the default. Compilers may supports other language linkages. GCC for example supports extern “Java” which allows interfacing with java code (though that’s quite cumbersome). extern “C” … Read more

Why do members of a static class need to be declared as static? Why isn’t it just implicit?

I get asked questions like this all the time. Basically the question boils down to “when a fact about a declared member can be deduced by the compiler should the explicit declaration of that fact be (1) required, (2) optional, or (3) forbidden?” There’s no one easy answer. Each one has to be taken on … Read more

Constant expression initializer for static class member of type double

In C++03 we were only allowed to provide an in class initializer for static member variables of const integral of enumeration types, in C++11 we could initialize a static member of literal type in class using constexpr. This restriction was kept in C++11 for const variables mainly for compatibility with C++03. We can see this … Read more

When to use enums, and when to replace them with a class with static members?

Enums are great for lightweight state information. For example, your color enum (excluding blue) would be good for querying the state of a traffic light. The true color along with the whole concept of color and all its baggage (alpha, color space, etc) don’t matter, just which state is the light in. Also, changing your … Read more