Menu

latest commits: "dynamic binding" FP with function pointers

Martin
2022-03-17
2022-03-21
  • Martin

    Martin - 2022-03-17

    Hi,

    due to the latest commits some FPs of this kind show up:

    Code:

    foo::foo(const std::string &prefix)
        : m_DynLogger(prefix.c_str())
    {
        m_LogInterface.pfDiagnostic = &logDiagnostic;
        m_LogInterface.pfError = &logError;
    }
    

    Output:

    [foo.cpp:23] -> [foo.hpp:45]: (style) Virtual function 'logDiagnostic' is called from constructor 'VocalizerLogInterface(const std::string&prefix)' at line 23. Dynamic binding is not used.
    [foo.cpp:24] -> [foo.hpp:51]: (style) Virtual function 'logError' is called from constructor 'VocalizerLogInterface(const std::string&prefix)' at line 24. Dynamic binding is not used.
    

    Could you please check?

    Thanks,
    Martin

    P.S.: I've copy-pasted this quickly, maybe in this reduced example cppcheck won't detect the member functions as virtual, but the problem should be clear anyway.

     
  • CHR

    CHR - 2022-03-17

    This code reproduces the problem, but is only acepted by gcc with -fpermissive:

    struct B {
        B();
        virtual void f() {}
    };
    struct S { void (B::*pf)() = nullptr; };
    B::B() {
        S s;
        s.pf = &f;
    }
    

    There is no warning for standard-compliant s.pf = &B::f;.

     
  • CHR

    CHR - 2022-03-21

    https://github.com/danmar/cppcheck/pull/3913 has been merged, does this fix your problem?

     

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.