Menu

const member functions detection

dcb
2020-05-08
2020-05-08
  • dcb

    dcb - 2020-05-08

    For this C++ source code:

    class S
    {
    private:
    int fred;

    public:
    S( int n) : fred( n) { };
    ~S() {};

    int getFred() { return fred; };
    

    };

    where the member function body is only a return statement and so the member
    function could be made const, cppcheck doesn't detect this situation.

    $ /home/dcb/cppcheck/trunk/cppcheck --enable=all may8e.cc
    may8e.cc:14:0: style: The function 'getFred' is never used. [unusedFunction]
    $

    I am not sure if this is a feature request or a bug report.

     
  • Daniel Marjamäki

    can you try with inconclusive also.

    The warning is inconclusive because there can be situations when a method could technically be const, but for design reasons you would like that it is not.

     
  • dcb

    dcb - 2020-05-08

    It works fine with --inconclusive flag.

    $ /home/dcb/cppcheck/trunk/cppcheck --enable=all --inconclusive may8e.cc
    may8e.cc:14:6: style:inconclusive: Technically the member function 'S::getFred' can be const. [functionConst]
    int getFred() { return fred; };
    ^
    may8e.cc:14:0: style: The function 'getFred' is never used. [unusedFunction]

    ^
    $

    Fast work. Thanks very much for your help.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.