2

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

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
Aaron Mampáro
  • 249
  • 2
  • 4
  • 13

3 Answers3

1

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.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
1

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.

Mark B
  • 95,107
  • 10
  • 109
  • 188
0

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.

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • If I understand correctly, the asker is asking whether he can call the copy-constructor from the assignment operator function. I don't see a problem in that, however I do the opposite because the copy constructor is more general and requires initialization of stuff. Like if you copy an array container. Before you proceed with other functions in your new object, you have to initialize it. – The Quantum Physicist Apr 04 '13 at 13:29
  • Somehow, I can't imagine how you can call the copy constructor inside the `operator=`, nor a standard constructor. But I may be too tired and I may be missing something. Partially OK about the opposite - call `operator=` inside the copy constructor, but I didn't get your point about the initialization and stuff. Anyway. – Kiril Kirov Apr 04 '13 at 13:34
  • *"Somehow, I can't imagine how you can call the copy constructor inside the `operator=`"* - Why not, that's a common idiom (even if you don't really call it yourself usually, especially in light of C++11). *"Partially OK about the opposite - call `operator=` inside the copy constructor"* - Nah, don't do that, that's rubbish. Other than that I also absolutely didn't get what this answer is saying. But nevermind, the question has changed significantly anyway. – Christian Rau Apr 04 '13 at 14:16