0

The function below works with the return type Class, Class& and void. So, what's the difference between each?

//deep copies one object into another
Club& Club::operator= (Club& obj) {
    this->members_ = std::unique_ptr <Member[]> (new (std::nothrow) Member[obj.num_members_]);
    if (members_ != nullptr) {
        this->num_members_ = obj.num_members_;

        for (int i = 0; i < obj.num_members_; i++) {
            this->members_[i] = obj.members_[i];
        }
    }

    else {
        std::cout << "Memory could not be allocated.\n";
        this->num_members_ = 0;
    }

    return *this;
}

My professor told me to return by value but I dont understand why we need to return anything?

In the main file,

club200 = club200a;

As we are directly making changes to the variables pointed by this, why do we need to return the address or the value? We're not storing the returned value anywhere.

Also, I understand that reference is used to chain the operator but what's really happening to the returned value?

happy
  • 1
  • 1
  • If you don't return something, it would be impossible to chain assignments e.g. `club1 = club2 = club3;`. Returning `Club &` enables that. Returning `Club` by value enables that inefficiently. – Peter Jun 21 '23 at 02:05
  • `members_ != nullptr` is always true even if `obj.num_members_` is zero. See more info https://stackoverflow.com/questions/1087042/c-new-int0-will-it-allocate-memory – 273K Jun 21 '23 at 02:11
  • It's a common idiom in C++ to `return *this;` as a reference, just as you're doing. That mimics the behavior of primitives, and assignment chaining. Returning a copy would be non-idiomatic, and inefficient. Returning nothing — `void` — would be non-idiomatic. There are a few arguably good reason to return nothing, and for my non-value classes (that are identity classes) I intentionally have the `void` return type for the assignment operator (unless I've `= delete` the assignment operator away altogether). – Eljay Jun 21 '23 at 02:13
  • **My professor told me to return by value** I think you misunderstood them. They likely told about use cases other than the assignment operator. – 273K Jun 21 '23 at 02:15
  • @273K I don't think so because he reduced marks for returning by reference instead of value, so I wondered if it was wrong or something. – happy Jun 21 '23 at 02:21
  • 1
    Oversimplified, a function that creates an object should return by value. Returning a reference is wrong because the local object will be destroyed and the reference will point at nonsense. That’s called a “dangling reference”. A function that modifies an existing object can return a reference to that object; the object still exists after the function returns. That’s what a conventional assignment operator does. – Pete Becker Jun 21 '23 at 02:25
  • Did they also tell you not to use exceptions? – n. m. could be an AI Jun 21 '23 at 04:45
  • Also `Club& Club::operator= (Club& obj)` should be `Club& Club::operator= (const Club& obj)` otherwise it won't work with a `Club` rvalue or a const lvalue. – doug Jun 21 '23 at 05:23

0 Answers0