开发者

inheriting from a class declarations in c++

When you want to inherit from a class in C++, is it illegal to have std declared in the first line below?

#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

This is assuming you have #include at the top. I get compile errors for having std::run_time_error, but do not seem to by doing it the second way and was wondering why.

 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


Both are legal. But assuming this is in a header file, you should not use the using directive version, as it places the name in the global namespace, which may cause problems for users of your header.

Edit: Just noticed that you have the class name wrong:

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

is what you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜