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<iostream>
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<B *>(&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