Bug 11174 - derived class can access protected base class member function through pointer to member function
Summary: derived class can access protected base class member function through pointer...
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3
: P2 normal
Target Milestone: 3.4.0
Assignee: Giovanni Bajo
Keywords: accepts-invalid, documentation, monitored
: 12119 14894 15308 15468 (view as bug list)
Depends on:
Reported: 2003-06-12 12:59 UTC by Mike Reed
Modified: 2004-05-25 14:38 UTC (History)
7 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2003-08-18 04:44:20


Note You need to log in before you can comment on or make changes to this bug.
Description Mike Reed 2003-06-12 12:59:56 UTC
Reading specs from /usr/gcc3local/lib/gcc-lib/i686-pc-linux-gnu/3.3/specs
Configured with: ../gcc-3.3/configure --prefix=/usr/gcc3local
Thread model: posix
gcc version 3.3

With the following code:

class A {
    void foo() {}

class B : public A {
    void bar() {
        A a;
        void (A::*pmf)() = &A::foo;  // 1
        (a.*pmf)();                  // 2

Compilation gives:
g++ -W -Wall -ansi -pedantic -c pmf.cpp
pmf.cpp: In member function `void B::bar()':
pmf.cpp:3: error: `void A::foo()' is protected
pmf.cpp:12: error: within this context

Compilation exited abnormally with code 1 at Thu Jun 12 10:28:26

But note that I am able to call the protected member function of the external
object via a pointer to member function (PMF).

Looking at the C++ spec (11.5 example see void D2::mem...) I think the line
labelled 1 should be ill-formed but since the compiler doesn't think so I am
able to gain access to the protected function on the line labelled 2.
Comment 1 Giovanni Bajo 2003-06-12 15:36:40 UTC
Confirmed. Reduced snippet:

struct A 
    int a;

struct B : A 
    void foo() 

This is accepted by any version of GCC but it is ill-formed (see the example in 
Comment 2 Kriang Lerdsuwanakij 2003-08-18 14:03:47 UTC
Patch submitted:

Comment 3 CVS Commits 2003-08-19 12:55:05 UTC
Subject: Bug 11174

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	lerdsuwa@gcc.gnu.org	2003-08-19 12:55:00

Modified files:
	gcc/cp         : ChangeLog init.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/g++.old-deja/g++.jason: access17.C 
Added files:
	gcc/testsuite/g++.dg/parse: access4.C access5.C 

Log message:
	PR c++/11174
	* init.c (build_offset_ref): Perform access checking for
	pointer to member correctly.
	* g++.dg/parse/access4.C: New test.
	* g++.dg/parse/access5.C: Likewise.
	* g++.old-deja/g++.jason/access17.C: Adjust error message.


Comment 4 Kriang Lerdsuwanakij 2003-08-19 12:55:52 UTC
Fixed in the main trunk.
Comment 5 Andrew Pinski 2003-09-01 03:54:07 UTC
*** Bug 12119 has been marked as a duplicate of this bug. ***
Comment 6 Andrew Pinski 2004-04-08 18:43:07 UTC
*** Bug 14894 has been marked as a duplicate of this bug. ***
Comment 7 Andrew Pinski 2004-05-06 12:36:20 UTC
*** Bug 15308 has been marked as a duplicate of this bug. ***
Comment 8 Andrew Pinski 2004-05-15 18:25:37 UTC
*** Bug 15468 has been marked as a duplicate of this bug. ***
Comment 9 Andrew Pinski 2004-05-17 19:17:37 UTC
Reopening for documentation.
Comment 10 Giovanni Bajo 2004-05-17 19:30:14 UTC
I'll take care of writing up changes.html/bugs.html for this. It affects more 
than pointer-to-members, and appears to be a FAQ.
Comment 11 Andrew Pinski 2004-05-25 14:38:24 UTC
Closing this one as PR 14949 is the one for the documenation.