Y
ymost
Hi,
I have a class with a friend function that returns an instance of a
private nested class:
//--------------------------------------------------
class A {
class B {};
friend B func(double x);
};
A::B func(double x) {return A::B();}
int main() {
func(1.2);
return 0;
}
//------------------------------------------------
This code compiles and runs on the MS Visual C++ 2008 compiler.
However, if I use a class template instead:
//--------------------------------------------------
template<typename T> class A {
class B {};
friend B func(T x);
};
template<typename T> typename A<T>::B func(T x) {return A<T>::B();}
int main() {
func(1.2); // <-- error received here
return 0;
}
//------------------------------------------------
I get an error:
error C2248: 'A<T>::B' : cannot access private class declared in class
'A<T>'.
Notice the error is in 'main', and not in the function definition, so
I don't think the problem is with the friend declaration.
So is returning an instance of a private nested class legal or not?
I have a class with a friend function that returns an instance of a
private nested class:
//--------------------------------------------------
class A {
class B {};
friend B func(double x);
};
A::B func(double x) {return A::B();}
int main() {
func(1.2);
return 0;
}
//------------------------------------------------
This code compiles and runs on the MS Visual C++ 2008 compiler.
However, if I use a class template instead:
//--------------------------------------------------
template<typename T> class A {
class B {};
friend B func(T x);
};
template<typename T> typename A<T>::B func(T x) {return A<T>::B();}
int main() {
func(1.2); // <-- error received here
return 0;
}
//------------------------------------------------
I get an error:
error C2248: 'A<T>::B' : cannot access private class declared in class
'A<T>'.
Notice the error is in 'main', and not in the function definition, so
I don't think the problem is with the friend declaration.
So is returning an instance of a private nested class legal or not?