C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 27th, 2009, 09:53 AM
yoni162 yoni162 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 19 yoni162 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 1 m 48 sec
Reputation Power: 0
Classes - '=' operator must be non-static member function?

I have this class:
Code:
class myClass { 
public: 
  myClass () {} 
  int get_field () const {return x;} 
  void set_x (int y) {x = y;} 
private: 
  int x; 
}; 


I want to write the '=' operator for this class. At first I tried defining it outside the class, like this:
Code:
void operator= (myClass& a, myClass& b)
{
       a.set_x(b.get_field());
}

but the compiler says "`void operator=(myClass&, myClass&)' must be a nonstatic member function ". Why can't the assignment operator be defined outside the class?

Thanks

Reply With Quote
  #2  
Old July 27th, 2009, 04:00 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 296 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 30 m 11 sec
Reputation Power: 14
Any function defined outside a class definition needs to have :: operator ( SCRO ).
Example:
Class A
{
...
public foo();
..
};

void A :: foo()
{
//foo's definition.

}

Reply With Quote
  #3  
Old July 28th, 2009, 04:37 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 13
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Quote:
Originally Posted by Cirus
Any function defined outside a class definition needs to have :: operator
Correction: Any member function.


Why can't the assignment operator be defined outside the class?
(My interpretation of the question: Why can't the assignment operator not be a member function?)

Probably because the default assignment operator won't be overridden that way.
__________________
Quote:
Programming by Coincidence
Fred types in some more code, tries it, and it still seems to work. [Then] the program suddenly stops working. [...] Fred doesnít know why the code is failing because he didnít know why it worked in the first place.
Undefined behavior results in: (worst to best)
-Erases your harddisk. Really.
-Appears to work - for now
-Delayed errors/crashes
-Crashes
-Compiler warning

Last edited by MaHuJa : July 28th, 2009 at 04:44 AM.

Reply With Quote
  #4  
Old July 28th, 2009, 11:09 AM
yoni162 yoni162 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 19 yoni162 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 1 m 48 sec
Reputation Power: 0
OK thanks for the answers (both of you). Cirus, I tried writing it like that the first time and it still won't compile, said that the assignment operator must take only one argument. That leads me to believe that MaHuJa is right, I guess the assignment operator cannot be defined/overloaded as a non-member function.
I have another question, see if I don't have to open a new thread for it: why does the assignment operator have to return a reference/copy of the object in order for it to support multiple assignments (for example a=b=c)? To be more clear, here's one example:
Code:
myClass& operator= (const myClass& a)
        {
            x=a.x; y=a.y;
            return *this;
        }
Here's another example:
Code:
void operator= (const myClass& a)
        {
            x=a.x; y=a.y;
        }

With the second "=" operator, lines like "a=b=c" generate a compilation error, while with the first, everything is fine. Why is that? If in the first example it returns 'void', it doesn't mean that if I wrote "b=c", 'b' is now void, right? so why won't it work that way?

Reply With Quote
  #5  
Old July 29th, 2009, 03:07 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 13
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
In a=(b=c) the subexpression b=c returns a void. So basically, it'd be looking for something along the lines of a.operator=(void) - which is illegal to make.

And the parantheses were just added for clarity.

Reply With Quote
  #6  
Old July 29th, 2009, 12:43 PM
yoni162 yoni162 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 19 yoni162 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 1 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by MaHuJa
In a=(b=c) the subexpression b=c returns a void. So basically, it'd be looking for something along the lines of a.operator=(void) - which is illegal to make.

And the parantheses were just added for clarity.

Alright thanks a lot.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Classes - '=' operator must be non-static member function?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap