0

I have a question about data structures. Is there a way to edit the whole structure at once, instead of editing every variable one at a time? FI:

struct foo
{
  int a=5;
  int b=4;
  int c=8;
};
int main()
{
    foo f;

    f-1;

    return 0;

}

Result:

  f.int a=4;
  f.int b=3;
  f.int c=7;

If there was a way to do this it would help mi a lot in a specific projevt i'm making. Anyway, thanks for you're time and help"D

Adam W.
  • 89
  • 7

2 Answers2

7

You're probably looking for operator overloading.

struct foo
{
  int a=5;
  int b=4;
  int c=8;
  foo operator-(int val) const {
      foo copy(*this);
      copy.a -= val;
      copy.b -= val;
      copy.c -= val;
      return copy;
  }
};
int main()
{
    foo f;

    f = f - 1;

    return 0;

}

You might also look into valarray if you don't know the number of parameters at compiletime.

Xirema
  • 19,889
  • 4
  • 32
  • 68
  • so i can use this time after time? fi in a loop? for(;;){f=f-1;}? – Adam W. Nov 07 '17 at 21:59
  • 1
    @AdamW. Naturally. You may want to [find a good book for teaching C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), since it seems like you might be missing some of the fundamentals. – Xirema Nov 07 '17 at 22:16
  • @PasserBy I don't see any of that as being a reason not to use `valarray`, I see it as a reason why `valarray` isn't as popular. Not really the same thing. – Xirema Nov 08 '17 at 01:24
  • The reason is twofold. First it's annoying to use, people are less accustomed to it and it has caveats, meaning it is harder to maintain. The second is it is likely even slower for certain scenarios. Both are quite compelling. – Passer By Nov 08 '17 at 01:50
  • @PasserBy yes you're right. I'm a big novice in c++, and i'm learning many things by asking questions like this. Anyway thank you for you're time and help:D – Adam W. Nov 08 '17 at 14:30
  • @AdamW. Thank Xirema, he did the explaining. I'm pointing out my disagreement on `valarray` – Passer By Nov 08 '17 at 14:31
  • @PasserBy I wanted to thank you for the advice. I always really appreciate when someone helps me on these kind of forums in any given way. – Adam W. Nov 08 '17 at 14:37
1

The answer by Xirema already answers your question.

I just want to point out that if you support f-1 as an operator function, there are several operators that you should also support.

Fortunately, you can re-use some of the implementations to implement others.

Here's my suggestion:

struct foo
{
   int a=5;
   int b=4;
   int c=8;

   // Pre-increment
   foo& operator++()
   {
      ++a;
      ++b;
      ++c;
      return *this;
   }

   // Post-increment
   foo operator++(int)
   {
      foo copy = *this;
      ++(*this);
      return copy;
   }

   // Pre-decrement
   foo& operator--()
   {
      --a;
      --b;
      --c;
      return *this;
   }

   // Post-decrement
   foo operator--(int)
   {
      foo copy = *this;
      --(*this);
      return copy;
   }

   // Increment
   foo& operator+=(int v)
   {
      a += v;
      b += v;
      c += v;
      return *this;
   }

   // Decrement
   foo& operator-=(int v)
   {
      // Use the inrement operator.
      return ( (*this) += (-v));
   }

   // Addition
   foo operator+(int v) const
   {
      foo copy = *this;
      copy += v;
      return copy;
   }

   // Subtraction
   foo operator-(int v) const
   {
      // Use the addition operator.
      return ((*this) + (-v));
   }
};

Test code:

int main()
{
   foo f1;

   // Pre-increment
   ++f1;

   // Post-increment
   f1++;

   // Pre-decrement
   --f1;

   // Post-decrement
   f1--;

   // Increment
   f1 += 10;

   // Decrement
   f1 -= 20;

   // Addition
   foo f2 = f1 + 20;

   // Subtraction
   foo f3 = f2 - 50;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270