Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class design: arrays vs multiple variables

I have a bit of a theoretical question, however it is a problem I sometimes face when designing classes and I see it done differently when reading others code. Which of the following would be better and why:

example 1:

class Color
{
public:
  Color(float, float, float);
  ~Color();

  friend bool operator==(Color& lhs, Color& rhs);
  void multiply(Color);
  // ...
  float get_r();
  float get_g();
  float get_b();

private:
  float color_values[3];
}

example 2:

class Color
{
public:
  // as above

private:
  float r;
  float g;
  float b;
}

Is there a general rule one should follow in cases like this or is it just up to a programmer and what seems to make more sense?

like image 654
jaho Avatar asked Jul 04 '12 15:07

jaho


3 Answers

Both!

Use this:

class Color {

    // ...

private:

    union {
       struct {
           float r, g, b;
       };
       float c[3];
    };

};

Then c[0] will be equivalent to r, et cetera.

like image 126
Kos Avatar answered Sep 20 '22 02:09

Kos


It depends, do you intend to iterate over the whole array ?

In that case, I think solution 1 is more appropriate. It is very useful to have an array like that when you have functions that operate in a loop on the data

e.g.

void BumpColors(float idx)
{
    for (int i = 0; i < 3; ++i)
        color_values[i] += idx;
}

vs

void BumpColors(float idx)
{
    color_values[0] += idx;
    color_values[1] += idx;
    color_values[2] += idx;
}

Of course this is trivial, and I think it really is a matter of preference. In some rare occasion you might have APIs that take a pointer to the data though, and while you can do

awesomeAPI((float*)&r);

I would much prefer doing

awesomeAPI((float*)&color_values[0]);

because the array will guarantee its contiguity whereas you can mess up with the contiguity by adding by mistake another member variable that is not related after float r.


Performance wise there would be no difference.

like image 25
Julien Lebot Avatar answered Sep 20 '22 02:09

Julien Lebot


I'd say the second one is the best one.

First, the data your variables contain isn't supposed (physically) to be in an array. If you had for example a class with 3 students, not more, not less, you'd put them in an array, cause they are an array of students, but here, it's just colors.

Second, Someone that reads your code also can understand in the second case really fast what your variables contain (r is red, etc). It isn't the case with an array.

Third, you'll have less bugs, you won't have to remember "oh, in my array, red is 0, g is 1, b is 2", and you won't replace by mistake

return color_values[0]

by

return color_values[1]

in your code.

like image 25
Tuxer Avatar answered Sep 21 '22 02:09

Tuxer