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

Passing “this” to a function from within a constructor?

When you instantiate an object in C++, the code in the constructor is the last thing executed. All other initialization, including superclass initialization, superclass constructor execution, and memory allocation happens beforehand. The code in the constructor is really just to perform additional initialization once the object is constructed. So it is perfectly valid to use … Read more

Initialize() vs Constructor() method, proper usage on object creation

I think there are multiple aspects that should be taken into consideration: A constructor should initialize an object in a way that it’s in a usable state. A constructor should only initialize an object, not perform heavy work. A constructor should not directly or indirectly call virtual members or external code. So in most cases … Read more

Passing object as parameter to constructor function and copy its properties to the new object?

You could do this. There is probably also a jquery way… function Box(obj) { for (var fld in obj) { this[fld] = obj[fld]; } } You can include a test for hasOwnProperty if you’ve (I think foolishly) extended object function Box(obj) { for (var fld in obj) { if (obj.hasOwnProperty(fld)) { this[fld] = obj[fld]; } … Read more

JavaScript: using constructor without operator ‘new’

In general, if something is documented as being a constructor, use new with it. But in this case, RegExp has a defined “factory” behavior for the situation where you’ve called it as a function instead. See Section 15.10.3 of the ECMAScript (JavaScript) specification (that links to the outgoing spec; the section number is the same … Read more