LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   (C++) operator = overloading with gcc 4.0.1 (https://www.linuxquestions.org/questions/programming-9/c-operator-%3D-overloading-with-gcc-4-0-1-a-591798/)

Omni 10-14-2007 07:35 PM

(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

matthewg42 10-14-2007 09:15 PM

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;
        }
};


dmail 10-14-2007 10:16 PM

Quote:

Originally Posted by Omni (Post 2924214)
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.

Omni 10-15-2007 03:29 PM

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.

matthewg42 10-15-2007 04:06 PM

If elegance, simplicity, beauty and consistency are what you seek, C++ is the wrong language. Just my 2 pennies.

dmail 10-15-2007 05:11 PM

Quote:

Originally Posted by Omni (Post 2925245)
<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?

Omni 10-15-2007 07:46 PM

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

dmail 10-16-2007 06:30 AM

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.


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