This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Befriending a private member function
- From: Herwig, BjÃrn <herwig at gdsys dot de>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Wed, 6 May 2009 09:45:34 +0200
- Subject: Befriending a private member function
Hello,
I recently came across a rather historic piece of C++ code once written for G++ 3 point
something. It doesn't compile with a G++ 4, as it tries to befriend a private member
function. Okay, this is easily solved by befriending the whole class, but I am really
curious now, why this isn't allowed anymore.
I've read the corresponding sections in "The C++ Programming Language, Special Edition"
and section 11.4 of the C++ 2003 standard, but that still left me clueless.
Could anybody give me a hint?
Best,
BjÃrn A. Herwig
-----
#include <string>
using namespace std;
class A
{
public:
void pubFunc()
{
}
private:
void privFunc(int)
{
}
};
class B
{
public:
friend void A::privFunc(int);
};
int main(int argc, char** argv)
{
B b;
return 0;
}
-----
-----
# g++ --version
g++ (GCC) 3.2.3 20030422 (Gentoo Linux 1.4 3.2.3-r1, propolice)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# g++ -Wall -o test test.cpp
test.cpp: In function `int main(int, char**)':
test.cpp:26: warning: unused variable `B b'
-----
-----
# g++ --version
g++ (Debian 4.3.3-3) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# g++ -Wall -o test test.cpp
test.cpp:13: error: âvoid A::privFunc(int)â is private
test.cpp:21: error: within this context
test.cpp: In function âint main(int, char**)â:
test.cpp:26: warning: unused variable âbâ
-----