error: `struct A' is inaccessible

J

jheled

Staring with g++ 3.4, this code results in an error

----------------------------------

struct A {
};

class B : private A {
};

class C : public B {
C(A& a);
};
---------------------------------

b.cc:2: error: `struct A' is inaccessible
b.cc:9: error: within this context

This looks wrong to me. You can't access B as an A, but why C can't use
the public A independently? what do other
compliers say?

(Think of the plausible usage
============
struct A {
};

class B : private A {
public:
B(A&) {}
};

class C : public B {
C(A& a) : B(a) {}
};
============
)


Can someone explain this?

Thanks, Joseph
 
A

Andrey Tarasevich

Staring with g++ 3.4, this code results in an error

----------------------------------

struct A {
};

class B : private A {
};

class C : public B {
C(A& a);
};
---------------------------------

b.cc:2: error: `struct A' is inaccessible
b.cc:9: error: within this context

This looks wrong to me. You can't access B as an A, but why C can't use
the public A independently? what do other
compliers say?

This is called "class name injection". Every time you define class 'A',
name 'A' is implicitly inserted ("injected") into the scope of 'A' as a
public member (see 9/3). In is loosely equivalent to

class A {
public:
typedef ::A A; // (informally, just a sketch)
...
};

Now, every time you refer to unqualified 'A' inside 'A' or any class
derived from 'A' directly or indirectly, name lookup will find 'A::A',
not the global 'A'.

(Note: an explicit attempt to use 'A::A' in the program will be treated
as a reference to the constructor, not to the injected class name).

That's exactly what happens in your case. When you refer to 'A' in
constructor declaration of 'C', name lookup finds the injected 'A::A',
which is inaccessible in 'C'.

You can work around this by using qualified name '::A'

class C : public B {
C:):A& a);
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top