Struct Questions

Hello,

Is there a way to relate structure objects?

For example:

struct Example () {
int a, b, c;

} Obj, Obj1, Obj2;

Obj1.a = 3;
Obj1.b = 1
Obj2.a = 4;
Obj2.b = 6;

I know that assigning all of an objects variables to another object works:
Obj = Obj1;

My question is: Can you manipulate all of the variables of an object in comparison to another? Like:

Obj = Obj1 + Obj2;

I know that particular line of code is not working. How would I do this without individually assigning each variable?

Thanks!


Two ways I can think of off the top of my head are:

A.) A loop that goes through each object and fills in the data. This won't be very efficent.

B.) The way I would do it: http://www.cplusplus.com/doc/tutorial/classes2/#static
Remeber "struct" and "class" are almost the same thing.
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.
@ Henrik: Or unless you use the "::" operator to declare the function as a member of the class\struct. Right below the first example here: http://www.cplusplus.com/doc/tutorial/classes/
Thanks for the replies. I'm still not quite grasping how I could get:

Obj = Obj1 + Obj2;

to add all of the variable values assigned to each object.

Inside my example struct, would you include your code per variable?:

struct Example () {
int a, b, c;

int operator+(Example example) {
return this->a + example.a;
return this->b + example.b;
return this->c + example.c;

}
void operator=(Example example) {
this->a = example.a;
this->b = example.b;
this->c = example.c;
}

} Obj, Obj1, Obj2;

And then:

Obj = Obj1 + Obj2;

I tried this, but it's not working
Last edited on
Give this a look over: http://www.cplusplus.com/doc/tutorial/classes2/
I don't think I can explain it better then this sites tutorial.

Like Henrik said, these are still functions. So if they return something then they need to be told what to return. In your case the operators would return "Example" not "void" or "int".
Last edited on
This is how you should code your struct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Example
{
    int a, b, c;

    //Add appropriate constructors.
    //Default constructor:  Sets variables to zero.
    Example() : a(0), b(0), c(0)
    { }
    //Constructor that allows setting all values at once:
    Example(int newA, int newB, int newC) : a(newA), b(newB), c(newC)
    { }
    //Now you are ready to overload operator+.  Note the return type.
    Example operator+(Example const &op2) const
    {
        //Here I make use of the second constructor.  Look how simple this is.
        return Example(a + op2.a, b + op2.b, c + op2.c);
    }
    //No copy constructor or assignment operator overloading needed.  The compiler-provided versions are good enough.
};
There we go! Thanks guys. Thanks webJose for making it easy by providing a working example. Now I can pick it apart and assign the variables accordingly. Thanks again!
Topic archived. No new replies allowed.