我想使用 C++98 和 g++ 编译器将一个类标记为已弃用,以便在直接使用此类或有人从此类派生时收到警告。
显然, using__attribute__ ((__deprecated__))
在使用类时有效,但不适用于继承。
例如:
#if defined(__GNUC__)
# define DEPRECATED __attribute__ ((__deprecated__))
#else
# define DEPRECATED
#endif
class DEPRECATED Foo
{
public:
explicit Foo(int foo) : m_foo(foo) {}
virtual ~Foo() {}
virtual int get() { return m_foo; }
private:
int m_foo;
};
class Bar : public Foo // This declaration does not produce a compiler
// warning.
{
public:
explicit Bar(int bar) : Foo(bar), m_bar(bar) {}
virtual ~Bar() {}
virtual int get() { return m_bar; }
private:
int m_bar;
};
int main(int argc, char *argv[])
{
Foo f(0); // This declaration produces a warning.
Bar b(0); // This declaration does not produce a warning.
return b.get();
}
我希望收到来自“class Bar:public Foo”的警告,但事实并非如此(使用 g++ 5.2.1 测试)。
从已弃用的类派生时,有没有办法发出警告?