473,395 Members | 1,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Operator= overloading question

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::operator=, 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::operator=(const Zoot& rhs)
{
if( this != &rhs )
{
delete [] _data;
_data = new int[_size];
}
return *this;
}
Oct 1 '05 #1
5 1137

"Ook" <Don't send me any freakin' spam> wrote in message
news:h9********************@giganews.com...
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::operator=, 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
Oct 1 '05 #2

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Jm****************@newsread3.news.pas.earthli nk.net...

"Ook" <Don't send me any freakin' spam> wrote in message
news:h9********************@giganews.com...
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::operator=, 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'
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
Oct 1 '05 #3
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::operator=(const Zoot& rhs)
{
if( this != &rhs )
{
// Make _size 3 times as big
_size *= 3;
delete [] _data;
_data = new int[_size];
}

return *this;

}
Oct 1 '05 #4

"Ook" <Don't send me any freakin' spam> wrote in message
news:44********************@giganews.com...

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::operator=(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
Oct 1 '05 #5
Mike Wahler wrote:
"Ook" <Don't send me any freakin' spam> wrote in message
news:44********************@giganews.com...
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::operator=(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
Oct 8 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
2
by: victor75040 | last post by:
Before you all start flaming me, I am not a student and this is not for any homework. Just someone learing c++ on their own. I am now up to the chapter in my book that describes operator...
2
by: Bo Sun | last post by:
hi: in the following code: class plus{ int data_item; public:
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
1
by: Tony Johansson | last post by:
Hello! I have this wrapper class Integer below that I use when testing operator overloading. A book that I read say that the expression Integer i; i+5 is translated to operator+(i,5) using the...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
6
by: jay | last post by:
In the c++ primer ,i get a program. A class's name is TT,and it define the operator overload! TT first; //constructor TT second(30);//constructor TT thrid(40://constructor...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
24
by: Rahul | last post by:
Hi Everyone, I was just overloading operator = for a class and i have a problem in one case... class A { A& operator=(const A& obj) { return *this;
3
by: Thomas Lenz | last post by:
The code below should allow to use a comma instead of << with ostreams and include a space between two operands when comma is used. e.g. cout << "hello", "world", endl; should print the line...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.