E
Eric Lilja
Hello, consider this program:
class Outer
{
public:
class Base
{
friend class Outer;
public:
Base(int n) : n_(n) {}
protected:
int n_;
};
class Child1 : public Base
{
friend class Outer;
friend class Child2;
public:
Child1(int n) : Base(n) {}
};
class Child2 : public Base
{
friend class Outer;
public:
Child2(int n) : Base(n) {}
Child2(const Child1& rhs) : Base(rhs.n_) {}
};
};
int main()
{
Outer::Child1 c1(4711);
Outer::Child2 c2(c1);
}
It compiles with MSVC++ 8.0 SP1 and gcc 4.2.0. It does not compile
with gcc 3.4.4:
$ g++ -Wall -Wextra -std=c++98 -pedantic -g foo.cpp -o runme.exe
foo.cpp: In constructor `Outer::Child2::Child2(const Outer::Child1&)':
foo.cpp:10: error: `int Outer::Base::n_' is protected
foo.cpp:27: error: within this context
Also, worth mentioning is that gcc 4.2.0 doesn't need quite as many
friend declarations as MSVC++ 8.0 SP1 does...is this code valid
according to the standard and is it being rejected by the older gcc
because of a bug or a failure to follow the standard as closely as gcc
4.2.0?
It's not an academic question for me, I encountered this problem in a
real program and this my reduced test case.
- Eric
class Outer
{
public:
class Base
{
friend class Outer;
public:
Base(int n) : n_(n) {}
protected:
int n_;
};
class Child1 : public Base
{
friend class Outer;
friend class Child2;
public:
Child1(int n) : Base(n) {}
};
class Child2 : public Base
{
friend class Outer;
public:
Child2(int n) : Base(n) {}
Child2(const Child1& rhs) : Base(rhs.n_) {}
};
};
int main()
{
Outer::Child1 c1(4711);
Outer::Child2 c2(c1);
}
It compiles with MSVC++ 8.0 SP1 and gcc 4.2.0. It does not compile
with gcc 3.4.4:
$ g++ -Wall -Wextra -std=c++98 -pedantic -g foo.cpp -o runme.exe
foo.cpp: In constructor `Outer::Child2::Child2(const Outer::Child1&)':
foo.cpp:10: error: `int Outer::Base::n_' is protected
foo.cpp:27: error: within this context
Also, worth mentioning is that gcc 4.2.0 doesn't need quite as many
friend declarations as MSVC++ 8.0 SP1 does...is this code valid
according to the standard and is it being rejected by the older gcc
because of a bug or a failure to follow the standard as closely as gcc
4.2.0?
It's not an academic question for me, I encountered this problem in a
real program and this my reduced test case.
- Eric