【发布时间】:2012-03-28 03:36:15
【问题描述】:
这段代码在VS2008中生成C2248 : 'A::B::ExceptionB' : cannot access private class declared in 'class A::B'。
#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;
}
}
当然,如果我在B 中公开ExceptionB{} 类,代码就会编译。
但我不明白为什么编译器不抱怨main() 中的两个catch 子句,因为A::ExceptionA 是A 中的私有类,而A::B::ExceptionB 是A::B 中的私有类.
【问题讨论】:
标签: c++ visual-c++ try-catch nested-class private-members