此代码在VS2008中生成C2248 : 'A::B::ExceptionB' : cannot access private class declared in 'class A::B'
。为什么编译器不会抱怨catch子句?
#include <iostream>
class A
{
class ExceptionA{};
class B
{
class ExceptionB{};
public:
B();
};
public:
A(int);
};
A::B::B()
{
throw ExceptionB();
}
A::A(int i)
{
i % 2 ? throw ExceptionA() : throw A::B::ExceptionB(); // C2248 !!
}
int main()
{
try
{
A a(3);
}
catch(A::ExceptionA&)
{
std::cout << "A::ExceptionA" << std::endl;
}
catch(A::B::ExceptionB&)
{
std::cout << "A::B::ExceptionB" << std::endl;
}
}
当然,如果我让类ExceptionB{}
公众B
,代码编译。
但我不明白为什么编译器简化版,在main()
抱怨的2个catch子句,为A::ExceptionA
是A
的私有类和A::B::ExceptionB
是A::B
私有类。
你说得对。如果我用'catch(A :: ExceptionA&a)'替换'catch(A :: ExceptionA&)',编译器会抱怨。您在标准中提到了第15.6章,但我找不到它,至少在标准最新草案N3225中找不到。你能确认15.6段落1吗? – Belloc 2012-03-11 15:27:38
@ user1042389:我在这里查看C++ 03。 – 2012-03-11 15:31:01
谢谢。我给你的答案的功劳。 – Belloc 2012-03-11 15:33:10