Why / how does the base class virtuality change the behavior of the copy constructor?

I do not understand the behavior of this fragment: (compiled with clang ++ 3.0)

#include <iostream> using namespace std; class Base { public: virtual void bar() {} bool foo = false; }; class Derived : public Base{ public: Derived() { Base::foo = true; } }; int main() { Derived d; Base b(d); cout << b.foo << endl; // prints 0 // prints 1 if "virtual void bar() {}" is removed } 

Why does the function Base :: bar () have any effect on copying Base :: foo?

+4
source share
1 answer

Your problem is similar to the one presented as an error here in the official bugzilla for the llvm project.

As you can see, this is a recognized bug, and it has been fixed in new versions of Clang, you must switch to a newer version of the interface to fix this problem; the bug report also indicates an exact revision of clang, which offers a fix for this problem.

+6
source

Source: https://habr.com/ru/post/1494969/


All Articles