This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/41721] New: C++ method resolution error


If I declare a class and extend it, the object created from the extended class
shouldn't always refer to the extended class methods and typeid even if casted
to the first class?

See the code:

#include <iostream>
#include <typeinfo>

class A
{
        public:
                int a;
                A() { ; }
                ~A() { ; }
                void foo() { std::cout << "A: foo! " << this->a << std::endl; a
+= 1000; }
};

class B : public A
{
        public:
                void foo() { std::cout << "B: foo! " << this->a << std::endl; }
};

int
main( int argc, char * argv[] )
{
        B * b = new B();
        A * a = NULL;

        b->a = 10;
        b->foo();
        a = (A *) b;
        a->foo();
        std::cout << typeid( a ).name() << std::endl;
        std::cout << typeid( b ).name() << std::endl;
        b->foo();
        a = NULL;

        delete b;

        return 0;
}

Actual result:
B: foo! 10
A: foo! 10
P1A
P1B
B: foo! 1010

Expected result:
B: foo! 10
B: foo! 10
P1B
P1B
B: foo! 10


-- 
           Summary: C++ method resolution error
           Product: gcc
           Version: 4.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sergio at gruposinternet dot com dot br
 GCC build triplet: ?
  GCC host triplet: GNU/Linux
GCC target triplet: i386


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41721


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]