C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Accessing private member - bug or feature?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthias Hofmann
Guest





PostPosted: Wed Jun 15, 2005 10:23 am    Post subject: Accessing private member - bug or feature? Reply with quote



Hello!

I have stumbled into something that really confuses me. The following code
compiles without any problems under both VC++ 6 and 7:

class A
{
// Private nested class.
class B
{
public:
void g() {}
};

public:
// Return object of private class.
B f() { return B(); }
};

int main()
{
A().f().g(); // Accessing private member.

return 0;
}

Note that B is a private nested class of class A. I expected the code to be
rejected, however I can call A::B::g(). Is this legal? Of course one might
interpret the declaration of A::f() returning an object of type B as an
agreement to bypass the private declaration.

I also found out that VC++ 6 does not reject the following either, while
VC++ 7 does:

int main()
{
A::B();

return 0;
}

I did not know that there were such severe problems in VC++ 6...

--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
diginux
Guest





PostPosted: Wed Jun 15, 2005 10:41 pm    Post subject: Re: Accessing private member - bug or feature? Reply with quote



I think this would be legal, since A().f().g() really is just returning
g() from an "instance" of B(). With that said, the code is very
confusing, I hope you aren't planning on adopting it for anything :)

Quote:
class A
{
// Private nested class.
class B
{
public:
void g() {}
};

public:
// Return object of private class.
B f() { return B(); }
};

int main()
{
A().f().g(); // Accessing private member.

return 0;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ben Hutchings
Guest





PostPosted: Wed Jun 15, 2005 10:51 pm    Post subject: Re: Accessing private member - bug or feature? Reply with quote



Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> wrote:
Quote:
Hello!

I have stumbled into something that really confuses me. The following code
compiles without any problems under both VC++ 6 and 7:

class A
{
// Private nested class.
class B
{
public:
void g() {}
};

public:
// Return object of private class.
B f() { return B(); }
};

int main()
{
A().f().g(); // Accessing private member.

return 0;
}

Note that B is a private nested class of class A. I expected the code to be
rejected, however I can call A::B::g(). Is this legal?

Yes. Accessibility checks apply to individual member name lookups and
not to the complete qualified names for the members they find. Since
the lookup that finds member A::B is done within A::f(), it is legal,
and since the lookup that finds A::B::g() does not name A::B, it is
also legal.

Quote:
Of course one might
interpret the declaration of A::f() returning an object of type B as an
agreement to bypass the private declaration.

Exactly.

Quote:
I also found out that VC++ 6 does not reject the following either, while
VC++ 7 does:

int main()
{
A::B();

return 0;
}

I did not know that there were such severe problems in VC++ 6...

That *is* a bug in VC++ 6.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Greg
Guest





PostPosted: Wed Jun 15, 2005 11:06 pm    Post subject: Re: Accessing private member - bug or feature? Reply with quote



Matthias Hofmann wrote:
Quote:
Hello!

I have stumbled into something that really confuses me. The following code
compiles without any problems under both VC++ 6 and 7:

class A
{
// Private nested class.
class B
{
public:
void g() {}
};

public:
// Return object of private class.
B f() { return B(); }
};

int main()
{
A().f().g(); // Accessing private member.

return 0;
}

Note that B is a private nested class of class A. I expected the code to be
rejected, however I can call A::B::g(). Is this legal? Of course one might
interpret the declaration of A::f() returning an object of type B as an
agreement to bypass the private declaration.

I also found out that VC++ 6 does not reject the following either, while
VC++ 7 does:

int main()
{
A::B();

return 0;
}

I did not know that there were such severe problems in VC++ 6...


The statement:

A().f().g();

is invoking three public methods so there is no access violation.
Whether class B's declaration is private or not is irrelevant.

Greg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.