Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22175

strange dynamic_cast behavior when target variable has same name with source variable

$
0
0
#include <iostream>#include <string>using namespace std;struct Foo {    virtual ~Foo() = default;    int foo = 321;};struct Bar: public Foo {    int bar = 123;};int main() {    const Foo* ptr = new Bar();    std::cout << ptr << std::endl;    {        const Bar* ptr = dynamic_cast<const Bar*>(ptr);        std::cout << ptr << std::endl;    }    {        const Bar* ptr2 = dynamic_cast<const Bar*>(ptr);        std::cout << ptr2 << std::endl;    }    return 0;}

the program output the following lines if compiled with -O2:

0x613c2000x613c20

and if without -O2, it's like:

0x614c200x400b700x614c20

In both case, the second line seems problematic, when the target variable name conflicts with source variable name of dynamic_cast. I tried both gcc 4.8 and 5.2 and also clang 11.0.3 .

Anyone has clue about this?


Viewing all articles
Browse latest Browse all 22175

Trending Articles