Call parent function which is being overridden by child during constructor chain in JavaScript(ES6) [duplicate]

You seem to be operating under a misconception that there are two objects A and B when you are in the constructor of the derived class B. This is not the case at all. There is one and only one object. Both A and B contribute properties and methods to that one object. The value …

Read more

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

There are several differences you appear to have missed: Context manager get a chance to provide a new object just for the block you are executing. Some context managers just return self there (like file objects do), but, as an example, database connection objects could return a cursor object tied to the current transaction. Context …

Read more

Why is explicit allowed for default constructors and constructors with 2 or more (non-default) parameters?

One reason certainly is because it doesn’t hurt. One reason where it’s needed is, if you have default arguments for the first parameter. The constructor becomes a default constructor, but can still be used as converting constructor struct A { explicit A(int = 0); // added it to a default constructor }; C++0x makes actual …

Read more

Setup std::vector in class constructor

Just do: MyClass::MyClass(int m_size) : size(m_size), vec(m_size, 0) You already seem to know about initializer lists, why not initialize vector there directly? vec = new vector<int>(size,0); is illegal because new returns a pointer and in your case vec is an object. Your second option: vector<int> temp(size,0); vec = temp; although it compiles, does extra work …

Read more

Initialize field before super constructor runs?

No, there is no way to do this. According to the language specs, instance variables aren’t even initialized until a super() call has been made. These are the steps performed during the constructor step of class instance creation, taken from the link: Assign the arguments for the constructor to newly created parameter variables for this …

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