我想在基类中抛出自己的异常Exception.有一个虚拟方法print将被子类覆盖.我只捕获类型Exception&并使用print来获取特定错误.问题是,一旦我抛出一个子类的引用,它就像它是基类一样被捕获.
这是一个例子:
#include <iostream>
using namespace std;
class Exception
{
public:
virtual void print()
{
cout << "Exception" << endl;
}
};
class IllegalArgumentException : public Exception
{
public:
virtual void print()
{
cout << "IllegalArgumentException" << endl;
}
};
int main(int argc, char **argv)
{
try
{
IllegalArgumentException i;
Exception& ref = i;
cout << "ref.print: ";
ref.print();
throw ref;
}
catch(Exception& e)
{
cout << "catched: ";
e.print();
}
} …Run Code Online (Sandbox Code Playgroud) 我的问题是关于以下代码:
template <class...T>
class A
{
public:
template <class...S>
static void a() { }
};
template <class...T>
class B
{
public:
template <class...S>
void b()
{
A<T...>::a<S...>();
}
};
int main(int argc, char** argv)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我有一个A具有可变参数模板的类,并包含一个a具有另一个可变参数模板的静态方法.从其他地方(B在本例中为类)我有两组不同的可变参数模板我想传递给它A::a.
编译器(GCC 4.8.1)给出以下错误消息:
main.cpp: In static member function ‘static void B<T>::b()’:
main.cpp:16:22: error: expected primary-expression before ‘...’ token
A <T...>::a<S...>();
^
main.cpp:16:22: error: expected ‘;’ before ‘...’ token
Run Code Online (Sandbox Code Playgroud)
另请注意,当我将方法更改为b():
void b()
{ …Run Code Online (Sandbox Code Playgroud)