I have code in which I really want to call a virtual method from the constructor. I know that this is considered unsafe, and I know enough about object construction to also understand why . I also do not experience these problems . My code is currently working, and I think everything should be fine, but I want to make sure.
That's what I'm doing:
I have a class hierarchy, and there is a regular public function that, as usual, just moves on to a private virtual method. However, I want to call this public method when building my objects, because it fills all the data into the object. I will be absolutely sure that this virtual call comes from a leaf class, because using this virtual method from any other part of the class hierarchy simply does not make sense.
So, in my opinion, the creation of the object should be completed when I make a virtual call, and everything should be fine. Is there anything that could go wrong? I think I will have to note this part of the logic with some remarks to explain why this logic should never be moved to any of the base clans, although it seems that it can be moved. But besides the stupidity of other programmers, I have to be fine, right?
LiKao source share