Befriending a private member function
Herwig, Björn
herwig@gdsys.de
Wed May 6 07:45:00 GMT 2009
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’
-----
More information about the Gcc-help
mailing list