Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:09:22+00:00 2026-06-07T00:09:22+00:00

I have a bit of a theoretical question, however it is a problem I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:09:23+00:00Added an answer on June 7, 2026 at 12:09 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding how initialization vector works on cryptography. I have bit
I have a bit of an architecture problem here. Say I have two tables,
I have little bit longer question for you - but hope answer will be
I have a bit of a problem. I am trying to do the following
Note:I don't mean some theoretical question which don't have any implementation just languages that
I have bit of a strange problem. In have a MovieClip i resize, but
This will be a really theoretical question, just bear with me a bit. I
This may be a slightly theoretical question. I have a char array of bytes
I have a bit of code that fits theoretical prediction to experimental data, and
I have a bit of a problem and I am struggling to solve it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.