继承自c ++中的类声明

时间:2010-05-13 19:11:36

标签: c++ inheritance

当你想从C ++继承一个类时,在下面的第一行声明std是不合法的吗?

#ifndef HWEXCEPTION_H
#define HWEXCEPTION_H

#include <stdexcept>


class HWException : public std::run_time_error
{
    void testException(int num);
};

#endif

VS

using std::run_time_error
class MyClass : public run_time_error

假设您在顶部有#include。我得到std :: run_time_error的编译错误,但似乎不是第二种方式,并且想知道为什么。

 error C2039: 'run_time_error' : is not a member of 'std'
 'run_time_error' : base class undefined
1>main.cpp
 error C2039: 'run_time_error' : is not a member of 'std'
 error C2504: 'run_time_error' : base class undefined

1 个答案:

答案 0 :(得分:4)

两者都是合法的。但假设这是在头文件中,则不应使用using指令版本,因为它将名称放在全局名称空间中,这可能会导致标题用户出现问题。

修改:注意到您的班级名称错误:

#include <stdexcept>
class MyClass : public std::runtime_error {
};

是你需要的。

相关问题