Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call copy constructor from assignment operator function

I have a class with a point to dynamically allocated array, so I created copy constructor and assignment operator function. Since copy constructor and assignment operator function do the same work, I call copy constructor from the assignment operator function but get "error C2082: redefinition of formal parameter". I am using Visual Studio 2012.

// default constructor
FeatureValue::FeatureValue()
{
    m_value = NULL;
}

// copy constructor 
FeatureValue::FeatureValue(const FeatureValue& other)
{
    m_size = other.m_size;  
    delete[] m_value;
    m_value = new uint8_t[m_size];

    for (int i = 0; i < m_size; i++)
    {
        m_value[i] = other.m_value[i];
    }
}

// assignment operator function
FeatureValue& FeatureValue::operator=(const FeatureValue& other)
{
    FeatureValue(other); // error C2082: redefinition of formal parameter
    return *this;
}
like image 679
stackunderflow Avatar asked Jul 05 '13 02:07

stackunderflow


People also ask

Can you call copy constructor in assignment operator?

You can safely invoke the copy assignment operator from the constructor as long as the operator is not declared virtual.

What is the difference between invoking a copy constructor and using an assignment?

The difference between a copy constructor and an assignment operator is that a copy constructor helps to create a copy of an already existing object without altering the original value of the created object, whereas an assignment operator helps to assign a new value to a data member or an object in the program.

How is copy constructor invoked?

The copy constructor is invoked when the new object is initialized with the existing object. The object is passed as an argument to the function. It returns the object.


2 Answers

The offending line isn't what you think it is. It actually declares a variable other of type FeatureValue. This is because constructors to not have names and cannot be called directly.

You can safely invoke the copy assignment operator from the constructor as long as the operator is not declared virtual.

FeatureValue::FeatureValue(const FeatureValue& other)
    : m_value(nullptr), m_size(0)
{
    *this = other;
}

// assignment operator function
FeatureValue& FeatureValue::operator=(const FeatureValue& other)
{
    if(this != &other)
    {
        // copy data first. Use std::unique_ptr if possible
        // avoids destroying our data if an exception occurs
        uint8_t* value = new uint8_t[other.m_size];
        int size = other.m_size;  

        for (int i = 0; i < other.m_size; i++)
        {
            value[i] = other.m_value[i];
        }

        // Assign values
        delete[] m_value;
        m_value = value;
        m_size = size;
    }
    return *this;
}

This will works just dandy or you can use the typical guidelines for the copy & swap idiom suggested in Vaughn Cato's answer

like image 169
Captain Obvlious Avatar answered Sep 24 '22 12:09

Captain Obvlious


You can't directly call a constructor like you would any other method. What you are doing is actually declaring a variable called other of type FeatureValue.

Take a look at the copy-and-swap idiom for a good way to avoid duplication between the assignment operator and the copy constructor: What is the copy-and-swap idiom?

Even better, use a std::vector instead of new and delete. Then you don't need to write your own copy constructor or assignment operator.

like image 40
Vaughn Cato Avatar answered Sep 24 '22 12:09

Vaughn Cato