On 18.03.2016 23:55, Lynn McGuire wrote:
> Well, I just got educated on virtual method calls in constructors and
> destructors. None will be executed as you think so that functionality
> will not work.
On the contrary, in C++ virtual functions are executed in the same way
from constructors and destructors as from elsewhere.
And unlike Java and C#, in C++ it's safe.
So, generally, it Just Works™, in C++.
That answer is a bit of FUD: Fear, Uncertainty and Doubt. Silly in a
technical context. But good for harvesting some rep points at SO, which,
after all, is the absolutely greatest extant Herb Schildt area on the
Internets :)
> and
>
>
http://stackoverflow.com/questions/9114982/calling-virtual-method-from-destructor-workaround
Uhm, too long didn't read, but it's a typical X/Y-question: wanting to
do X, envisioning solution Y, finding that Y is impossible or
impractical (usually complete nonsense), asking about Y only.
> I devised a way to get around the virtual method call in the constructor
> by adding an argument to my base constructor which fixed that issue.
I guess you want to do derived class specific initialization WITHIN the
base class constructor, e.g. creating a Button API-level widget in the
constructor of a general Widget base class constructor.
Hurray, the FAQ is there for you!
https://isocpp.org/wiki/faq/strange-inheritance#calling-virtuals-from-ctor-idiom
Re terminology: AFAIK the “Dynamic Binding During Initialization” term
is just something Marshall Cline invented, it wasn't there in my
original FAQ suggestion, and I've never seen it anywhere else.
> But getting around the virtual method call in my destructor is painful.
That's the Y of an X/Y problem. ;-)
What is the /real/ problem, that you try to solve that way?
Cheers & hth.,
- Alf