J
Jerry Krinock
I'm writing a class which has, as members, dynamically allocated
valarrays. I'd like to overload the "=" operator so that, when
operating on two objects of my class, the valarray values of the rhs
will be assigned to the valarray values of the lhs.
Well, in doing so I have discovered something I don't understand about
this operator; it destroys my operands upon return. I've written the
following demo, simplified to use ints instead of valarrays:
#include <iostream>
using namespace std ;
class Foo
{
private:
char me ;
int* num ;
public:
Foo(char me_, int num_) //constructor
{
me = me_ ;
num = new int ;
*num = num_;
}
int GetNum() const // getter
{
return *num ;
}
Foo operator=(const Foo rhs)
{
*num = rhs.GetNum() ;
return *this ;
}
void print()
{
cout << me << " = " << *num << endl ;
}
~Foo()
{
cout << "Destroying " << me << endl ;
delete num ;
}
};
int main ()
{
Foo a( 'a', 2);
Foo b( 'b', 3);
cout << "Before operating..." << endl ;
a.print();
b.print();
a=b;
cout << "After operating..." << endl ;
a.print();
b.print();
cout << "All done!!" << endl ;
return 0;
}
Here is the output:
Before operating...
a = 2
b = 3
Destroying a
Destroying b
After operating...
a = 13691
b = 3
All done!!
Destroying b
*** malloc[683]: error for object 0x300470: Double free
Destroying a
*** malloc[683]: Deallocation of a pointer not malloced: 0x300460; This
could be........
Why does my overloaded operator= destroy its operands when it returns?
It doesn't even seem to care that I've declared the rhs as const!
Jerry Krinock
San Jose, CA USA
valarrays. I'd like to overload the "=" operator so that, when
operating on two objects of my class, the valarray values of the rhs
will be assigned to the valarray values of the lhs.
Well, in doing so I have discovered something I don't understand about
this operator; it destroys my operands upon return. I've written the
following demo, simplified to use ints instead of valarrays:
#include <iostream>
using namespace std ;
class Foo
{
private:
char me ;
int* num ;
public:
Foo(char me_, int num_) //constructor
{
me = me_ ;
num = new int ;
*num = num_;
}
int GetNum() const // getter
{
return *num ;
}
Foo operator=(const Foo rhs)
{
*num = rhs.GetNum() ;
return *this ;
}
void print()
{
cout << me << " = " << *num << endl ;
}
~Foo()
{
cout << "Destroying " << me << endl ;
delete num ;
}
};
int main ()
{
Foo a( 'a', 2);
Foo b( 'b', 3);
cout << "Before operating..." << endl ;
a.print();
b.print();
a=b;
cout << "After operating..." << endl ;
a.print();
b.print();
cout << "All done!!" << endl ;
return 0;
}
Here is the output:
Before operating...
a = 2
b = 3
Destroying a
Destroying b
After operating...
a = 13691
b = 3
All done!!
Destroying b
*** malloc[683]: error for object 0x300470: Double free
Destroying a
*** malloc[683]: Deallocation of a pointer not malloced: 0x300460; This
could be........
Why does my overloaded operator= destroy its operands when it returns?
It doesn't even seem to care that I've declared the rhs as const!
Jerry Krinock
San Jose, CA USA