GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Jul-2009, 16:03
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

What is the other way of calling the overloaded assignment operator?


in the code below.. what is the other way of calling the overloaded assignment operator? descriped in the commnets below. Also, this function returns a reference "&", this mean the copy constructor is called, correct?


CPP / C++ / C Code:
IntlPhoneNumberWS& IntlPhoneNumberWS::operator=(const IntlPhoneNumberWS& s){
   IntlPhoneNumber::operator=(s); //(IntlPhoneNumber&)operator=(s); not sure if this would work??
   if (this != &s){               //(IntlPhoneNumber&)(*this).operator=(s); or this, not sure??
      if (subscriber != NULL) delete [] subscriber;
      if (s.subscriber != NULL){
	 subscriber = new char[strlen(s.subscriber) + 1];
	 strcpy(subscriber, s.subscriber);
      }else subscriber = NULL;
   }
   return *this;
}


Thanks
Daniel
  #2  
Old 02-Jul-2009, 23:03
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Another Question?


Quote:
Originally Posted by nowocien
in the code below.. what is the other way of calling the overloaded assignment operator?
Quote:
//(IntlPhoneNumber&)(*this).operator=(s); or this, not sure??
If you are not sure, write a simple example to test your hypothesis.

Member functions (not static member functions...) require an object to call the member function. While the following:
Quote:
(*this).operator=(s);
...does take an object & calls a member function, why dereference this followed by using the dot (.) operator? Why not use -> as in the following:
Code:
this->operator=(s);
Writing simple test cases yourself can help you:
  • learn the syntax.
  • think about the consequences of the syntax at a deeper level.
  • will help you teach yourself without having to wait for someone else to do it for you.
Quote:
Also, this function returns a reference "&", this mean the copy constructor is called, correct?
No, not necessarily. The ampersand allows an object to be used as an lvalue (left-hand side of an assignment...) or rvalue (right-hand side of an assignment...). A copy constructor may be called in some instances, but this isn't a direct result of the signature of assignment operator overload as it is a consequence that an object is being initialized with the value of another object.
  #3  
Old 03-Jul-2009, 11:39
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

Re: What is the other way of calling the overloaded assignment operator?


ok thanks OCICAT, yeah the thing is i test so much stuff that some times its just easier to post a question and go back to it later, the responses on the forum are so fast that I got spoiled. I love this place. Yeah i know -> is better then (*this).operator but im just a begginer so I want to make sure I know all the possiblilites, other people have different style, i want to understand everything

Thanks AGain, this forum is the shittt!!
  #4  
Old 06-Jul-2009, 13:23
nowocien nowocien is offline
Awaiting Email Confirmation
 
Join Date: Nov 2008
Posts: 30
nowocien is an unknown quantity at this point

Re: What is the other way of calling the overloaded assignment operator?


Just wanted to add, for those that may be learning like me that:
Quote:
Also, this function returns a reference "&", this mean the copy constructor is called, correct?

the copy constructor is call when returning BY VALUE, passing BY VALUE to a function as a paramater and initialization of an object using the assigment '=' operator to another object as OSICAT pointed out.

Thanks
Daniel
  #5  
Old 04-Sep-2009, 10:03
CPlus2Progammer CPlus2Progammer is offline
New Member
 
Join Date: Sep 2009
Posts: 2
CPlus2Progammer is on a distinguished road

Re: What is the other way of calling the overloaded assignment operator?


in your code above i see you return an object on the free store (heap) "By Reference" or easy to say an "Alias" of Object on the free store (heap).

return *this; // right ? its mean you dereference the this pointer of current object its pointed to, an the result is the object on the free store (heap), not a copy constructor or constructor anymore.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please, This is Difficult question try for me! bcompt143 C++ Forum 4 24-Aug-2008 10:14
Question about locking surfaces to directly access pixels, SDL. george89 C++ Forum 0 18-Jun-2006 22:16
Question re "Similar Pages" on Google jep Search Engine Optimization Forum 0 17-May-2005 10:52
question of practice magiccreative C++ Forum 1 06-Feb-2004 08:17
Motherboard and CPU Question pcxgamer Computer Hardware Forum 2 29-Oct-2002 07:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:14.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.