Menu

Possible false positive Member variable '' is not assigned a value in 'operator='.

2024-10-09
2024-10-09
  • Jean Porcherot

    Jean Porcherot - 2024-10-09
    #include <memory>
    
    class MyClass
    {
        class MyClassPrivateData
        {
        public:
            int m_member;
    
            MyClassPrivateData& operator=( const MyClassPrivateData& priv )
            {
                this->m_member = priv.m_member;
                return *this;
            }
        };
    
        std::unique_ptr<MyClassPrivateData> m_data;
    
    public:
    
        MyClass() : m_data( new MyClassPrivateData() )
        {
    
        }
    
        MyClass(const MyClass&) = delete;
    
        // warning : Member variable 'MyClass::m_data' is not assigned a value in 'MyClass::operator='.
        MyClass& operator=( const MyClass& time )
        {
            if ( &time != this )
            {
                (*m_data) = *(time.m_data);
            }
            return *this;
        }
    };
    

    It is intended not to set m_data, as it's a unique_ptr( would be the same if being a raw pointer), Value pointed is assigned, not pointer itself

     
  • CHR

    CHR - 2024-10-09

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/13203

     

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.