Can someone explain this unexpected behavior?
Room
I created a Thread class containing the member std::thread
variable. Thread ctor creates a std::thread
element that provides a pointer to a static function that calls a pure virtual function (which will be implemented by the base classes).
The code
#include <iostream>
Problem
As you already noticed, there is a subtle error here: Thread::ThreadStart(...)
is called from the Thread
ctor context, so calling a pure / virtual function will not call the implementation of the derived class. This is confirmed by a runtime error:
pure virtual method called terminate called without an active exception Aborted
However, there is an unexpected runtime behavior if I remove the call to std::cout
in Thread
ctor:
virtual void {anonymous}::A::Run(){anonymous}::Verbose::Verbose(int): : 042 virtual void {anonymous}::A::Run(): 1 virtual void {anonymous}::A::Run(): 2 virtual void {anonymous}::A::Run(): 3 virtual void {anonymous}::A::Run(): 4
those. removing the call to std::cout
in Thread
ctor seems to have the effect of invoking the derived class of the "pure / virtual function from the context of the constructor of the base class"! This is not consistent with previous training and experience.
Create an environment in Cygwin x64 on Windows 10. gcc Version:
g++ (GCC) 5.4.0 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I am puzzled by this observation and grieving with curiosity about what is happening. Can anyone shed some light?
source share