Legacy class inheritance

I would like to mark the class as deprecated using C ++ 98 and the g ++ compiler to get a warning when this class is used directly or when someone comes from this class.

Usage seems to __attribute__ ((__deprecated__))work when a class is used, but not for inheritance.

For example:

#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();
}

I expect to receive a warning from the "class Bar: public Foo", but it is not (tested with g ++ 5.2.1).

Is there a way to get a warning when outputting from an obsolete class?

+4
source share
1 answer

, , , , , , , . , , .

class base {
  class DEPRECATED deprecator {};
public:
  base() {
    deprecator issue_warning;
    (void) issue_warning; // this line is no-op to silence unused variable warning for local variable
  }
};

class derived : public base {
public:
  derived()
    : base()
  {
    // I am also deprecated, because I called the ctor for base
    // which instantiates a deprecated class
  }
};

, base , , , . , derived, base - ctors , derived.

+5

Source: https://habr.com/ru/post/1606462/


All Articles