Aaron Mampáro
Aaron Mampáro

Reputation: 259

Calling constructor within overloaded assignment operator?

Basically, is it acceptable programming practice/style to call a constructor of a class within its overloaded assignment operator? If not, why not?

EXAMPLE:

so I have a class which has 3 data members, a dynamic int array called "value" which holds digits of a large number, an int length which indicates the number of digits in the number, & an int maxLength which indicates the max length of the number (size of the dynamic array)

here's my constructor with param int:

bigInt::bigInt(const int &rhs){
    //turn num_ into a string 'num'
    stringstream ss;
    ss << num_;
    string num = ss.str();
    length = strlen(num.c_str());
    maxLength = (length - (length%16)) + 16;
    value = new int[maxLength];
    for(int i=1; i<=length; i++){
        value[i-1] = num.at(length-i) - '0';
    }
}

and here's my overloaded assignment operator in which the righthand side is a regular int this method calls the constructor:

bigInt bigInt::operator=(const int &rhs){
    *this = bigInt(rhs);
    return *this;
}

EDIT: I guess I should have worded it differently. I didn't mean COPY constructor, but rather a regular constructor with non-class-instance parameter, and an overloaded assignment operator in which the rhs isn't the same type as the lys

Upvotes: 3

Views: 1653

Answers (3)

Mark B
Mark B

Reputation: 96233

This is not an unreasonable way to implement your assignment operator, since it allows you to utilize existing code, and provides the strong guarantee as long as your copy assignment operator does.

Two points of note however: First, make sure that your copy constructor, copy assignment operator, and destructor are all implemented properly or you'll start having memory management problems. Second, the C++ language ALREADY provides a class with elements, a length, and a max length: It's called std::vector and if you change your code to use it, you don't need to write the copy constructor, copy assignment operator, and destructor at all, as they'll just behave properly!

Also, your assignment operator should return by reference (or if you really don't want chaining, void). Returning by value will someday cause a bug when a chained assignment doesn't work as expected.

Upvotes: 1

James Kanze
James Kanze

Reputation: 153899

There's nothing wrong with calling the copy constructor in the implementation of an assignment operator (unless the copy constructor itself is implemented in terms of assignment, of course). In fact, it's a common idiom: make a local copy, then swap the data with the current instance. (This, of course, supposes that you have a specialized swap. Just calling std::swap on the object itself in an assignment operator is not a good idea. Creating a new instance, and then swapping the individual components of the object often is. Or creating a custom swap function which swaps the components, and calling it.)

Upvotes: 1

The Quantum Physicist
The Quantum Physicist

Reputation: 26256

The opposite is better. It's perfectly fine.

However, don't forget that in the copy contructor you MUST redo what you do in the constructor; i.e., initialize any variables you have in your class, which is not necessary to be done in the overloaded assignment operator function.

Upvotes: 0

Related Questions