Operator= overloading question

O

Ook

Given the following code, when I do Zoot v3 = v1, it does not execute the
operator= function, instead it executes my copy constructor. Why is this,
and how do I get it to execute my overloaded operator= function instead?
Everything looks all right, it compiles, it runs as expected except for the
v3 = v1. I expected it to call Zoot::eek:perator=, but when I trace through it,
it calls the copy contstructor instead.

Class Zoot {
public:
// Constructors
Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }
Zoot(const Zoot& vec);

// Overload operator=
Zoot& operator=(const Zoot& rhs);

private:
int _size;
int* _data;
}
...........

int main()
{
Zoot v1(5);
Zoot v2( v1 );
Zoot v3 = v1;
return 0;
}

// Copy constructor
Zoot::Zoot(const Zoot& vec)
{
// Make the new _size twice as big as the existing one
_size = vec._size * 2;
// Initialize _data
_data = new int[ _size ];
}

// Assignment operator overload
Zoot& Zoot::eek:perator=(const Zoot& rhs)
{
if( this != &rhs )
{
delete [] _data;
_data = new int[_size];
}
return *this;
}
 
M

Mike Wahler

Ook said:
Given the following code, when I do Zoot v3 = v1, it does not execute the
operator= function, instead it executes my copy constructor. Why is this,
and how do I get it to execute my overloaded operator= function instead?
Everything looks all right, it compiles, it runs as expected except for
the v3 = v1. I expected it to call Zoot::eek:perator=, but when I trace
through it, it calls the copy contstructor instead.

The statment:

Zoot v3 = v1;

Performs the creation and initialization of a
type 'Zoot' object. The initial value being
assigned to 'v3' is the value of 'v1'. No assignment
is taking place. It has exactly the same meaning as:

Zoot v3(v1);

Assignment is when an already existing object receives
a new value:

Zoot v3;
Zoot v1;
v3 = v1; /* assignment */

Zoot v4 = v1 /* copy construction ('v4' is being created (constructed) ) */

-Mike
 
M

Mike Wahler

Mike Wahler said:
The statment:

Zoot v3 = v1;

Performs the creation and initialization of a
type 'Zoot' object.
The initial value being
assigned to 'v3'

That should read:

The value with which 'v3' is being initialized is that of 'v1'.
No assignment
is taking place. It has exactly the same meaning as:

Zoot v3(v1);
-Mike
 
O

Ook

Assignment is when an already existing object receives
a new value:

Zoot v3;
Zoot v1;
v3 = v1; /* assignment */

Mike, thanks - that was a bit obvious, but I just didn't see it.

Next question, if I may: In my operator=, I have this. My question, is why
do you delete [] _data? Can't we resize/reuse the existing _data?

Zoot& Zoot::eek:perator=(const Zoot& rhs)
{
if( this != &rhs )
{
// Make _size 3 times as big
_size *= 3;
delete [] _data;
_data = new int[_size];
}

return *this;

}
 
M

Mike Wahler

Ook said:
Assignment is when an already existing object receives
a new value:

Zoot v3;
Zoot v1;
v3 = v1; /* assignment */

Mike, thanks - that was a bit obvious, but I just didn't see it.

Next question, if I may: In my operator=, I have this. My question, is why
do you delete [] _data? Can't we resize/reuse the existing _data?


Zoot& Zoot::eek:perator=(const Zoot& rhs)
{
if( this != &rhs )
{
// Make _size 3 times as big
_size *= 3;
delete [] _data;
_data = new int[_size];
}

return *this;

}

The 'new' and 'delete' mechanism doesn't do 'resizing'
like C's 'realloc()'. The only way to change an allocation
size is to de-allocate the old one and allocate again with the new
size.

-Mike
 
S

sjbrown8

Mike said:
Assignment is when an already existing object receives
a new value:

Zoot v3;
Zoot v1;
v3 = v1; /* assignment */

Mike, thanks - that was a bit obvious, but I just didn't see it.

Next question, if I may: In my operator=, I have this. My question, is why
do you delete [] _data? Can't we resize/reuse the existing _data?


Zoot& Zoot::eek:perator=(const Zoot& rhs)
{
if( this != &rhs )
{
// Make _size 3 times as big
_size *= 3;
delete [] _data;
_data = new int[_size];
}

return *this;

}


The 'new' and 'delete' mechanism doesn't do 'resizing'
like C's 'realloc()'. The only way to change an allocation
size is to de-allocate the old one and allocate again with the new
size.

-Mike
The std::vector template automates such deallocation and reallocation to
give the illusion of resizing, i believe.

-Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top