LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 10-14-2007, 07:35 PM   #1
Omni
Member
 
Registered: Sep 2003
Posts: 54

Rep: Reputation: 15
(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
 
Old 10-14-2007, 09:15 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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;
        }
};
 
Old 10-14-2007, 10:16 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by Omni View Post
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.
 
Old 10-15-2007, 03:29 PM   #4
Omni
Member
 
Registered: Sep 2003
Posts: 54

Original Poster
Rep: Reputation: 15
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.
 
Old 10-15-2007, 04:06 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
If elegance, simplicity, beauty and consistency are what you seek, C++ is the wrong language. Just my 2 pennies.
 
Old 10-15-2007, 05:11 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by Omni View Post
<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.
 
Old 10-15-2007, 07:46 PM   #7
Omni
Member
 
Registered: Sep 2003
Posts: 54

Original Poster
Rep: Reputation: 15
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
 
Old 10-16-2007, 06:30 AM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help overloading 'new' operator c++ qwijibow Programming 1 03-07-2007 04:58 PM
Problems with overloading operator+ in C++ niteshadw Programming 13 07-20-2005 07:45 PM
c++ [] operator overloading exodist Programming 12 04-17-2004 03:06 PM
c++ overloading input operator true_atlantis Programming 4 02-25-2004 07:24 PM
C++ overloading extraction operator petercool Programming 5 09-06-2003 06:35 AM


All times are GMT -5. The time now is 08:43 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration