First Last Prev Next    No search results available      Search page      Enter new bug
Bug#: 20397
Product:  
Component:  
Status: RESOLVED
Resolution: INVALID
Assigned To: Not yet assigned to anyone <unassigned@gcc.gnu.org>
Host:
Reported against  
Priority:  
Severity:  
Target Milestone:  
 
 
Target:
Reporter: matthew.whitney@gmail.com
Add CC:
CC:
Remove selected CCs
Build:
URL:
Summary:
Keywords:
Known to work:
Known to fail:

Attachment Description Type Created Size Actions
Create a New Attachment (proposed patch, testcase, etc.) View All

Bug 20397 depends on: Show dependency tree
Show dependency graph
Bug 20397 blocks:

Additional Comments:






View Bug Activity   |   Format For Printing   |   Clone This Bug


Description:   Last confirmed: Opened: 2005-03-09 21:24
Example code:

class A {
};

class B : A {
};

class C : B {
public:
	int c(A *a) { return 7; }
};

int main() { 
	
	A a;
	C c;

	return c.c( &a );
}

Gives the error message:

g++ main.cc
	main.cpp:1: `class A' is inaccessible
	main.cpp:9: within this context

Which is true, but not relevant. C never tries to access A through B.

------- Comment #1 From Andrew Pinski 2005-03-09 21:49 -------
No gcc is correct because the type A is interjected into B. and is private as
the base class is private.

------- Comment #2 From matthew.whitney@gmail.com 2005-03-10 02:25 -------
Could someone take another look at this? I don't mean to waste your time, but I
don't understand the explanation given. I know of another compiler that handles
the example code without any problems.

I agree that 'A is inaccessible' from C, but the code doesn't try to access it.
Consider this one word change to class C:

class C : B {
public:
	int c(void *a) { return 7; }
};

Now it compiles fine. Before this is closed again I'd like to understand why it
should matter what type an unreferenced parameter is. Why shouldn't I be able to
pass a pointer to an object of type A (completely unrelated to C) to one of C's
methods? Thanks in advance.

------- Comment #3 From Andrew Pinski 2005-03-10 03:33 -------
Huh?  unused paramaters are still need to be able to compile and be accessible.

If that compiler handles this code, well that compiler has a bug in it.

ICC gives the following warning though (but note it says allowed for compatiblity):
t.cc(9): warning #525: type "A::A" is an inaccessible type (allowed for compatibility)


The point is A is inaccessible in the class C because it is interjected into B.  If you want to use the global 
class A, use ::A and that works.

------- Comment #4 From Daniel Jacobowitz 2005-03-10 03:45 -------
Subject: Re:  g++ rejects valid code with 'is inaccessible' error

On Thu, Mar 10, 2005 at 02:25:03AM -0000, matthew dot whitney at gmail dot com wrote:
> Now it compiles fine. Before this is closed again I'd like to understand why it
> should matter what type an unreferenced parameter is. Why shouldn't I be able to
> pass a pointer to an object of type A (completely unrelated to C) to one of C's
> methods? Thanks in advance.

You could if A were completely unrelated to C.  You have C deriving
from B, and B deriving from A, which means that the name A binds to the
base class inside C; and the base class is inaccessible, so it may not
be referred to.  As Andrew said, use ::A.

If you want chapter and verse from the C++ standard, I can't help you,
but I'm sure someone else here can.


------- Comment #5 From Giovanni Bajo 2005-03-10 06:56 -------
Just ask on comp.lang.c++.moderated for more information. We help with C++ 
issues a little, but we cannot explaint things in detail to every C++ 
developer in the world.

Ah, and when Andrew said "interjected" he really meant "injected" :)

------- Comment #6 From matthew.whitney@gmail.com 2005-03-10 14:10 -------
Thanks for the explanation - I understand what's happening now. Next time I'll
take it to comp.lang.c++.moderated before bothering you guys. Anyway sorry to
have wasted your time.

First Last Prev Next    No search results available      Search page      Enter new bug