Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-14-2007, 07:35 PM
|
#1
|
|
Member
Registered: Sep 2003
Posts: 54
Rep:
|
(C++) operator = overloading with gcc 4.0.1
Hey there, all,
I have the following code I am trying to compile with gcc v4.0.1:
Code:
struct vec3f
{
union
{
float x, y, z;
float v[3];
};
};
const vec3f &operator =(vec3f &lhs, const vec3f &rhs)
{
memcpy(&lhs, &rhs, sizeof(vec3f));
return lhs;
}
And I get the following error message:
Quote:
|
error: 'const vec3f& operator=(vec3f&, const vec3f&)' must be a nonstatic member function
|
But this doesn't make any sense to me. I can overload all other operators the same or similar way without error, except this. Why does this require the operator to be a part of a class? I don't think the Microsoft compiler errors in the same way...
I know I can just write a class and overload the operator, but that's not the point. The point is I don't understand what is going on here, and now I must know why.
Any insight would be delightful.
Regards,
Omni
|
|
|
|
10-14-2007, 09:15 PM
|
#2
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Try this:
Code:
struct vec3f
{
union
{
float x, y, z;
float v[3];
};
const vec3f& operator =(const vec3f &rhs)
{
memcpy(this, &rhs, sizeof(vec3f));
return *this;
}
};
|
|
|
|
10-14-2007, 10:16 PM
|
#3
|
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
Quote:
Originally Posted by Omni
Any insight would be delightful.
Regards,
Omni
|
The standard says it all
Quote:
12.8.10
10 If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. The implicitlydeclared
copy assignment operator for a class X will have the form
X& X:: operator =( const X&)
if
— each direct base class B of X has a copy assignment operator whose parameter is of type const B&, const
volatile B& or B, and
— for all the non-static data members of X that are of a class type M (or array thereof), each such class type has a
copy assignment operator whose parameter is of type const M&, const volatile M& or M.117)
Otherwise, the implicitly declared copy assignment operator will have the form
X& X:: operator =(X&)
The implicitly-declared copy assignment operator for class X has the return type X&; it returns the object for which the
assignment operator is invoked, that is, the object assigned to. An implicitly-declared copy assignment operator is an
inline public member of its class. Because a copy assignment operator is implicitly declared for a class if not declared
by the user, a base class copy assignment operator is always hidden by the copy assignment operator of a derived class
(13.5.3).
|
Quote:
13.5.3 Assignment
1 An assignment operator shall be implemented by a non-static member function with exactly one parameter. Because a
copy assignment operator operator= is implicitly declared for a class if not declared by the user (12.8), a base class
assignment operator is always hidden by the copy assignment operator of the derived class.
|
Last edited by dmail; 10-14-2007 at 10:23 PM.
|
|
|
|
10-15-2007, 03:29 PM
|
#4
|
|
Member
Registered: Sep 2003
Posts: 54
Original Poster
Rep:
|
matthewg42 didn't read my post:
Quote:
|
I know I can just write a class and overload the operator, but that's not the point.
|
<Rant>
I read the standards sections that dmail posted, and though they are indisputable, I still think they're stupid. I have a good idea, lets let the developers overload all operators as non-member functions, except the assignment operator. Let's just take consistency, and shoot it in the face! Doesn't that sound like fun?
</Rant>
<Semi-Rant>
I suppose it's all in how one interprets "shall be" as stated in 15.5.3. Usually such terms are defined in a standard, I should look it up. But anyhow, "shall be" is not the same as "must be".
</Semi-Rant>
Don't mind me, I'm just pissed because I think it stupid.
|
|
|
|
10-15-2007, 04:06 PM
|
#5
|
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
If elegance, simplicity, beauty and consistency are what you seek, C++ is the wrong language. Just my 2 pennies.
|
|
|
|
10-15-2007, 05:11 PM
|
#6
|
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
Quote:
Originally Posted by Omni
<Rant>
I read the standards sections that dmail posted, and though they are indisputable, I still think they're stupid. I have a good idea, lets let the developers overload all operators as non-member functions, except the assignment operator. Let's just take consistency, and shoot it in the face! Doesn't that sound like fun?
</Rant>
|
The reason is the first line of the standard's quote
Quote:
|
If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly...
|
Don't mind me, I'm just laughing because I think ...
You don't need an assignment operator as it is implicitly declared and you have a POD type, in addition I think I am correct in thinking that it uses a byte for byte copy of the structure which may even be exactly the same thing you have wrote. The copy constructor, default constructor and assignment operator are some of the building blocks for the language and the standard calls them "Special member functions".
Quote:
<Semi-Rant>
I suppose it's all in how one interprets "shall be" as stated in 15.5.3. Usually such terms are defined in a standard, I should look it up. But anyhow, "shall be" is not the same as "must be".
</Semi-Rant>
|
This is taken from a draft; yet lets change the wording then:
Quote:
|
An assignment operator must be implemented by a non-static member function with exactly one parameter.
|
What does this imply? That you must implement it?
Last edited by dmail; 10-15-2007 at 05:32 PM.
|
|
|
|
10-15-2007, 07:46 PM
|
#7
|
|
Member
Registered: Sep 2003
Posts: 54
Original Poster
Rep:
|
dmail,
I think you just made an unwarranted jump. The example code was just that, an example. I know the implicit assignment operator will do exactly the same thing I presented in the example.
But what if the data structures have pointers that need to be deep copied?
Anyway, in all this thread has been a nice mental exercise. The answer to which is that the implicit operator needs to be overloaded because the existence of a non member assignment operator would introduce an ambiguous situation wherein the compiler would not be able to deduce which operator to use. I didn't think of it in those terms earlier.
But thank you all who posted,
--Omni
|
|
|
|
10-16-2007, 06:30 AM
|
#8
|
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
Quote:
|
I think you just made an unwarranted jump.
|
I don't think so but anyway...
Quote:
|
But what if the data structures have pointers that need to be deep copied?
|
A general rule of thumb is that if you need to define one of the special member functions, you will more than likely will need to define the three of them.
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:26 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|