Remix.run Logo
▲ lefty2 a day ago

I usually need to use a Init() function to initialised the member functions, because they are initialised with data that isn't available when the constructor is called, making it largely redundant. Another thing, just because a feature exists in a language doesn't mean you need to use it. Use it if it makes sense for your use case, otherwise don't use it.

▲zubspace a day ago | parent [-]

This is something I struggle with alot.

Constructors make alot of sense to me. It's a promise to the caller that the instance is fully initialized after it returns.

But there are cases where it simply ain't possible. For exampIe I always struggle with object pools, where you construct a bare instance and reuse it later on. In such cases it is often very cumbersome to to guarantee that each instance is properly initialized and uninitialized in every case. It gets worse when different instances are involved, depending on each other. The more classes there are, the worse it gets.