Manipulating struct members with functions

Let's say I have the following struct:

1
2
3
4
5
struct foo
{
   int a;
   int b;
};


Now let's say I want to manipulate a and b. I could write the following functions:
1
2
3
4
5
6
7
8
9
void barA(foo &baz)
{
   //do stuff to baz.a
}

void barB(foo &baz)
{
   //do stuff to baz.b
}


But what I would like to do is just write one general function that could manipulate either a or b. I'm not sure how to write this function, though. I imagine pointers are involved, but it's been a while since I've used C++, and my skills are rusty. Could someone help me out?
Last edited on
Couldn't you just take an int&, and pass foo_instance.a or foo_instance.b?
Try using a class instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class foo
{
private:
    int a;
    int b;
public:
    //contstructor
    foo(int a_in, int b_in)
    {
        a = a_in;
        b = b_in;
    }
    //getters
    int getA() { return a; }
    int getB() { return b; }

    //setters
    void setA(int a_in)
    {
        //Do stuff to A here
    }
    void setB(int b_in)
    {
        //Do stuff to B here
    }
    void setAB(int a_in, int b_in)
    {
        //Do stuff to a and b here
    }
    void setAorB(int v_in, bool SetA)
    {
        if (Set A) a = v_in;
        else b = v_in;
    }
};


Then in your function you could use:
1
2
3
4
5
6
7
8
9
10
11
12
int x=0, y=0;
foo baz (x,y);

//If you want to set A only:
baz.setA( x );
//If you want to set B only:
baz.setB( y );
//You can also try this to set B only (depending on your code in the set function):
baz.setAB( baz.getA(), y);
//Or you can try inputing one value as shown here and then flag which variable you want to set with a bool:
baz.setAorB ( x , true );

I hope this helps. If we want to be able to manipulate A or B we'll need to tell the struct or class which one we need to modify anyways.
Last edited on
It appears that in my tired state last night, I did not correctly explain what the function should do. The function should be able to handle multiple instances of a struct type and be able to manipulate a specified combination of those structs' members. Ideally I'd like to be able to pass in the names of the members like this:

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

void bar(foo baz1, foo baz2, ??? member1, ??? member2)
{
   //do stuff to baz1.member1

   //do stuff to baz2.member2

   foo baz3;
   //do stuff to baz3.member1
   //do stuff to baz3.member2
}


So the problem is that I don't know what to replace the ???s with or if this is even possible. A variation of Stewbond's setAorB function would probably work, but this way would be cleaner if it's doable.
Last edited on
Can you please describe with pseudocode what it actually is you are trying to do?
How about:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct foo
{
   int a;
   int b;
   int c;
   int d;
};
int main ()
{
    foo MyStruct1, MyStruct2;
    void bar( &MyStruct1 , &MyStruct2 , member1 , member2 );
}
void bar(foo *baz1, foo *baz2, int member1, int member2)
{
   *baz1.a = member1;
   *baz2.b = member2;

   foo baz3;  //This structure will be destroyed when the '}' is encountered. Only declare local things here.
}
closed account (zb0S216C)
Maybe your could do something similar to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Bar( foo *, int, foo *, int );

int main( )
{
    foo X = { 0, 0 };
    foo Y = { 0, 0 };

    Bar( &X, 0, nullptr, 0 );
}

void Bar( foo *A, int ANew, foo *B, int BNew )
{
    if( A != nullptr )
        A->a = ANew;

    if( B != nullptr )
        B->b = BNew;
}


Wazzak
Topic archived. No new replies allowed.