using operator overloading

hey every one,
i have a class and it has a private member (Pixel) of structure type. the structure has 3 integer variables. basically im trying to make an class for image processing and i need 3 variables for Red Greed and Blue(ie make a 3D pixel).
now my question is can i overload the operator = , so i can assign a value (such as zero) to that private variable Pixel. so i dont have to make for loops and and do the value assigning for each of the inner variables.
i wanna have some thing like Pixel=0; instead of Pixel.r=0,Pixel.g=0,Pixel.b=0;
any idea?

closed account (zb0S216C)
Usually, it's good practice to only overload bitwise assignment operator if the bitwise operation isn't suitable for copying the values within the structure; a DMA'd variable, for instance.

1
2
3
4
5
6
struct POD
{
    int R, G, B;
} NewA, NewB;

A = B;

Since there's nothing particularly special about the data members of POD, a bitwise copy of them wouldn't yield any negative side-effects.

I've mentioned default assignment operator, but what is it? It's the default version of the assignment operator that acts upon the class type. The compiler will implicitly provide one, only if the assignment operator is used (the operator will be in-line and public). For example:

1
2
3
4
5
6
7
8
9
10
class Base
{
} A, B;

int main( )
{
    A;
    B;
    return( 0 );
}

The compiler will not generate a default assignment operator for Base, because the assignment operator was never needed.

Wazzak
Last edited on
Topic archived. No new replies allowed.