operator overload =

Hi everyone,
this is a question about the operator overload = .
i read the book and here is the way it shows.

class Car
{
public:
int getVal() {return *wheel;}
private:
int *wheel;
};

Car& Car::operator=(const Car & rhs)
{
if (this == &rhs)
return *this;
delete wheel;//do we have to clean up the memory(old contants of object)
wheel = new int;
*wheel = rhs.getVal();
return *this;
}

about delete wheel. do we have to free the memory first?. i mean why don't just assign the new value directly?

thank you for your time for this case.


Comments

  • [b][red]This message was edited by stober at 2006-12-28 15:31:26[/red][/b][hr]
    : Hi everyone,
    : this is a question about the operator overload = .
    : i read the book and here is the way it shows.
    :
    : class Car
    : {
    : public:
    : int getVal() {return *wheel;}
    : private:
    : int *wheel;
    : };
    :
    : Car& Car::operator=(const Car & rhs)
    : {
    : if (this == &rhs)
    : return *this;
    : delete wheel;//do we have to clean up the memory(old contants of object)
    : wheel = new int;
    : *wheel = rhs.getVal();
    : return *this;
    : }
    :
    : about delete wheel. do we have to free the memory first?. i mean why don't just assign the new value directly?
    :
    : thank you for your time for this case.
    :
    :
    :

    [blue]why is variable [b]wheel[/b] a pointer? No need to dynamically allocate just one integer. [/blue]





  • : [b][red]This message was edited by stober at 2006-12-28 15:31:26[/red][/b][hr]
    : : Hi everyone,
    : : this is a question about the operator overload = .
    : : i read the book and here is the way it shows.
    : :
    : : class Car
    : : {
    : : public:
    : : int getVal() {return *wheel;}
    : : private:
    : : int *wheel;
    : : };
    : :
    : : Car& Car::operator=(const Car & rhs)
    : : {
    : : if (this == &rhs)
    : : return *this;
    : : delete wheel;//do we have to clean up the memory(old contants of object)
    : : wheel = new int;
    : : *wheel = rhs.getVal();
    : : return *this;
    : : }
    : :
    : : about delete wheel. do we have to free the memory first?. i mean why don't just assign the new value directly?
    : :
    : : thank you for your time for this case.
    : :
    : :
    : :
    :
    : [blue]why is variable [b]wheel[/b] a pointer? No need to dynamically allocate just one integer. [/blue]
    :
    :
    [blue]
    To answer your question--Yes. Sort of. Your current code reallocates
    memory each time operator= is executed.

    Instead, why not allocate it [b]one[/b] time and delete it in the
    destructor? ie:[/blue][code]
    Car& Car::operator=(const Car & rhs)
    {
    if (!wheel)
    wheel = new int; //Allocate ptr one time only

    // if (this == &rhs) //Redundent test
    // return *this;

    *wheel = rhs.getVal();
    return *this;
    }[/code][blue]

    Second, I agree with Stober...[b]wheel[/b] should NOT be
    a pointer here.

    Hope this helps;

    ~mt2002[/blue]

  • : : [b][red]This message was edited by stober at 2006-12-28 15:31:26[/red][/b][hr]
    : : : Hi everyone,
    : : : this is a question about the operator overload = .
    : : : i read the book and here is the way it shows.
    : : :
    : : : class Car
    : : : {
    : : : public:
    : : : int getVal() {return *wheel;}
    : : : private:
    : : : int *wheel;
    : : : };
    : : :
    : : : Car& Car::operator=(const Car & rhs)
    : : : {
    : : : if (this == &rhs)
    : : : return *this;
    : : : delete wheel;//do we have to clean up the memory(old contants of object)
    : : : wheel = new int;
    : : : *wheel = rhs.getVal();
    : : : return *this;
    : : : }
    : : :
    : : : about delete wheel. do we have to free the memory first?. i mean why don't just assign the new value directly?
    : : :
    : : : thank you for your time for this case.
    : : :
    : : :
    : : :
    : :
    : : [blue]why is variable [b]wheel[/b] a pointer? No need to dynamically allocate just one integer. [/blue]
    : :
    : :
    : [blue]
    : To answer your question--Yes. Sort of. Your current code reallocates
    : memory each time operator= is executed.
    :
    : Instead, why not allocate it [b]one[/b] time and delete it in the
    : destructor? ie:[/blue][code]
    : Car& Car::operator=(const Car & rhs)
    : {
    : if (!wheel)
    : wheel = new int; //Allocate ptr one time only
    :
    : // if (this == &rhs) //Redundent test
    : // return *this;
    :
    : *wheel = rhs.getVal();
    : return *this;
    : }[/code][blue]
    :
    : Second, I agree with Stober...[b]wheel[/b] should NOT be
    : a pointer here.
    :
    : Hope this helps;
    :
    : ~mt2002[/blue]
    :
    :
    Thank you so much for the rapid reply. i was working and couldn't go online. it does not necessary to use pointer. i did some search and found out that most of the website which talking about assignment overload, they all using the heap. and all of them are delete and relocate the memory again and again. thank you, Stober. and mt2002, thanks for the suggestion. your code does a lot of improvement.

    Happy New Year to everyone.

    Hao
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories