Yes, classes and structures are almost completely similiar - the only difference is that class´ members are private by default and structure´s members are public by default.
My recommendation would be the use of operator overloading. Give a meaning to this:
Obj + Obj2
As you may be aware, this should return something; in this case it is value. You can program a solution for this like this:
1 2 3
|
int operator+(Example example){ // "Example" is the name of the struct
return this->a + example.a;
}
| |
This is will return substraction of two structures (does only work with our variable a).
Then, let´s move on to this:
Obj = Obj2
Now you can overload assignment operator like this:
1 2 3 4
|
void operator=(Example example){
this->a = example.a;
this->b = example.b;
}
| |
We don´t need to return anything, because it´s just assignment of different values.
Remember! These functions (after all, that´s what they are) must declared inside the structure you would like to use, otherwisely they don´t work, unless you use more global scope solution, but I wouldn´t recommend it.