Compiler messages with pub/prot/priv inheritance

E

Edd

Hello all,

Please consider:


class Base {
public: int pub;
protected: int prot;
private: int priv;
};

class D1 : public Base { };
class D2 : protected Base { };
class D3 : private Base { };

int main()
{
D1 d1; D2 d2; D3 d3;

d1.pub = 0; //fine - public
d1.prot = 0; //error - protected
d1.priv = 0; //error - private

d2.pub = 0; //error - protected
d2.prot = 0; //error - protected? ('inaccessible')
d2.priv = 0; //error - private

d3.pub = 0; //error - private? ('inaccessible')
d3.prot = 0; //error - protected
d3.priv = 0; //error - private

return 0;
}


I would have expected that the error messages for the lines I've tagged with
'inaccessible' to complain about d2.prot being protected and d3.pub being
private, as to my knowledge this is how they would act given their access
specifiers and the types of inheritance used.

However, both MinGW's g++ and VC++ Express 05 use the phrase "inaccessible" and
avoid "protected" and "private" (which are used in all the other erroneous
cases). Does this mean there's some kind of technical middle-ground between
public/protected/private or am I reading in to these error messages too much?! I
suspect the latter, but I just want to make sure.

Thanks for your patience, and happy new year.

Edd
 
M

Mike Wahler

Edd said:
Hello all,

Please consider:


class Base {
public: int pub;
protected: int prot;
private: int priv;
};

class D1 : public Base { };
class D2 : protected Base { };
class D3 : private Base { };

int main()
{
D1 d1; D2 d2; D3 d3;

d1.pub = 0; //fine - public
d1.prot = 0; //error - protected
d1.priv = 0; //error - private

d2.pub = 0; //error - protected
d2.prot = 0; //error - protected? ('inaccessible')
d2.priv = 0; //error - private

d3.pub = 0; //error - private? ('inaccessible')
d3.prot = 0; //error - protected
d3.priv = 0; //error - private

return 0;
}


I would have expected that the error messages for the lines I've tagged
with 'inaccessible' to complain about d2.prot being protected and d3.pub
being private, as to my knowledge this is how they would act given their
access specifiers and the types of inheritance used.

However, both MinGW's g++ and VC++ Express 05 use the phrase
"inaccessible" and avoid "protected" and "private" (which are used in all
the other erroneous cases). Does this mean there's some kind of technical
middle-ground between public/protected/private
No.

or am I reading in to these error messages too much?!


Absolutely. The standard cites circumstances (e.g. syntax
error) where a 'diagnostic' is required. There are absolutely
no rules governing what the text of such messages should be.

IOW a compiler can be considered conformant if, for any
kind of diagnostic-required error, it emits "Wrong!" and
nothing else. (Of course such a compiler would not last
long in the marketplace).
I suspect the latter, but I just want to make sure.

Now you are. :)

Because of the way compilers work, it can sometimes be
difficult to relate a given error message to a specific
construct.

This is solved via experience (with that
particular compiler).

e.g.
-Mike
 
E

Edd

Mike said:
> Now you are. :)

Because of the way compilers work, it can sometimes be
difficult to relate a given error message to a specific
construct.

This is what I suspected, thanks Mike. I can sleep easy now!

Edd
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top