From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18188 invoked by alias); 28 Jan 2005 09:20:58 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 17975 invoked by uid 48); 28 Jan 2005 09:20:51 -0000 Date: Fri, 28 Jan 2005 09:20:00 -0000 From: "amit_choudhary at mindtree dot com" To: gcc-bugs@gcc.gnu.org Message-ID: <20050128092049.19665.amit_choudhary@mindtree.com> Reply-To: gcc-bugzilla@gcc.gnu.org Subject: [Bug c++/19665] New: Can access private member function if it is virtual X-Bugzilla-Reason: CC X-SW-Source: 2005-01/txt/msg04133.txt.bz2 List-Id: I was doing some experiments and I discovered that I can access a private member function if I make it virtual and do few other tricks. The code is below. The "Base class called" is printed. So, basically when someone wants to access a virtual function (by explicit type casting), there is no check whether that virtual function is private or not. But if I try to call it directly, it checks that it is a private member function. May be g++ compiler needs to be modified. #include using namespace std; class A { private: virtual void abc() { cout << "Base Class called\n"; } }; class B : public A { public: void abc() { cout << "Derived Class called\n"; } }; int main() { A a; B *b = static_cast(&a); b->abc(); } [amit]$ g++ c++-bug.cpp c++-bug.cpp:6: warning: all member functions in class `A' are private [amit]$ ./a.out Base Class called [amit]$ -- Summary: Can access private member function if it is virtual Product: gcc Version: 3.3.3 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: amit_choudhary at mindtree dot com CC: gcc-bugs at gcc dot gnu dot org GCC build triplet: g++ c++-bug.cpp c++-bug.cpp:6: warning: all member functions in GCC host triplet: gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) GCC target triplet: Configured with: ../configure --prefix=/usr -- mandir=/usr/share/ http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19665