locked
Is it the right approach to explicitly define the missing copy constructor, move constructor, copy assignment operator and move assignment operator so that they will not be implicitly provided in C++ RRS feed

  • Question

  • Hi all,

    I have the below code

    In .h file

    class ClassB{

    private:
    ClassA*               pClassAHandler;
    public:
    ~ClassB(void);
    }

    In .cpp file

    ClassB::~ClassB()
    {
            if(nullptr != pClassAHandler)
            {
                delete pClassAHandler;
                pClassAHandler = nullptr;
            }
    }

    With that I am getting the below error:

    To resolve sonarqube error, Explicitly define the missing copy constructor, move constructor, copy assignment operator and move assignment operator so that they will not be implicitly provided.

    I am following rule of 5 and providing the below special functions (copy constructor, move constructor, copy assignment operator and move assignment operator):


      ClassB::ClassB(const ClassB &other) {
        pClassAHandler = new ClassA(other.pClassAHandler->value);
      }

      ClassB::ClassB& operator=(const ClassB& other) {   
        int val = other.pClassAHandler->value;
        delete pClassAHandler;
        pClassAHandler = new ClassA(val);
        return *this;
      }

      ClassB::ClassB(ClassB &&fp) noexcept {
        pClassAHandler = fp.pClassAHandler;
        fp.pClassAHandler = nullptr;
      }

      ClassB::ClassB const & operator=(ClassB &&fp) {
        ClassB temp(std::move(fp));
        std::swap(temp.pClassAHandler, pClassAHandler);
        return *this;
      }

    Do we need to define all these functions like as shown above?

    And also is it the right approach to define those functions? And will i get any other issues if i use those functions?

    Please help me on this?


    Thursday, May 21, 2020 2:34 PM

Answers

  • Hi all,

    I have the below code

    In .h file

    class ClassA{

    private:
    ClassA*               pClassAHandler;
    public:
    ~ClassB(void);
    }

    In .cpp file

    ClassB::~ClassB()
    {
            if(nullptr != pClassAHandler)
            {
                delete pClassAHandler;
                pClassAHandler = nullptr;
            }
    }

    With that I am getting the below error:

    To resolve sonarqube error, Explicitly define the missing copy constructor, move constructor, copy assignment operator and move assignment operator so that they will not be implicitly provided.

    I am following rule of 5 and providing the below special functions (copy constructor, move constructor, copy assignment operator and move assignment operator):


      ClassB::ClassB(const ClassB &other) {
        pClassAHandler = new ClassA(other.pClassAHandler->value);
      }

      ClassB::ClassB& operator=(const ClassB& other) {   
        int val = other.pClassAHandler->value;
        delete pClassAHandler;
        pClassAHandler = new ClassA(val);
        return *this;
      }

      ClassB::ClassB(ClassB &&fp) noexcept {
        pClassAHandler = fp.pClassAHandler;
        fp.pClassAHandler = nullptr;
      }

      ClassB::ClassB const & operator=(ClassB &&fp) {
        ClassB temp(std::move(fp));
        std::swap(temp.pClassAHandler, pClassAHandler);
        return *this;
      }

    Do we need to define all these functions like as shown above?

    And also is it the right approach to define those functions? And will i get any other issues if i use those functions?

    Please help me on this?

    Hello,

    your class has a pointer that is dynamically allocated.

    A standard copy/assignment/.. constructor or operator will only copy the address in the pointer. They will never allocate memory. So you must explicitly implement these functions.

    Regards, Guido

    Monday, May 25, 2020 6:13 AM

All replies

  • Hi,

    Thank you for posting here.

    >>To resolve sonarqube error, Explicitly define the missing copy constructor, move constructor, copy assignment operator and move assignment operator so that they will not be implicitly provided.

    Accodring to the issue, it is a sonarqube error.I suggest you should ask it in SonarSource Community. And this thread will be moved to Off-Topic Posts.

    Best Regards,

    Jeanine Zhang


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 22, 2020 1:56 AM
  • Hi all,

    I have the below code

    In .h file

    class ClassA{

    private:
    ClassA*               pClassAHandler;
    public:
    ~ClassB(void);
    }

    In .cpp file

    ClassB::~ClassB()
    {
            if(nullptr != pClassAHandler)
            {
                delete pClassAHandler;
                pClassAHandler = nullptr;
            }
    }

    With that I am getting the below error:

    To resolve sonarqube error, Explicitly define the missing copy constructor, move constructor, copy assignment operator and move assignment operator so that they will not be implicitly provided.

    I am following rule of 5 and providing the below special functions (copy constructor, move constructor, copy assignment operator and move assignment operator):


      ClassB::ClassB(const ClassB &other) {
        pClassAHandler = new ClassA(other.pClassAHandler->value);
      }

      ClassB::ClassB& operator=(const ClassB& other) {   
        int val = other.pClassAHandler->value;
        delete pClassAHandler;
        pClassAHandler = new ClassA(val);
        return *this;
      }

      ClassB::ClassB(ClassB &&fp) noexcept {
        pClassAHandler = fp.pClassAHandler;
        fp.pClassAHandler = nullptr;
      }

      ClassB::ClassB const & operator=(ClassB &&fp) {
        ClassB temp(std::move(fp));
        std::swap(temp.pClassAHandler, pClassAHandler);
        return *this;
      }

    Do we need to define all these functions like as shown above?

    And also is it the right approach to define those functions? And will i get any other issues if i use those functions?

    Please help me on this?

    Hello,

    your class has a pointer that is dynamically allocated.

    A standard copy/assignment/.. constructor or operator will only copy the address in the pointer. They will never allocate memory. So you must explicitly implement these functions.

    Regards, Guido

    Monday, May 25, 2020 6:13 AM