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 4081016
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:00:19+00:00 2026-05-20T18:00:19+00:00

I commonly run into the following situation where I have a data structure which

  • 0

I commonly run into the following situation where I have a data structure which I’d like to have access as follows:

class data {
public:
    double error;
    double value;

    ...
}

...

data *outputs;

...

double lastValue = ...;
double someValue = ...;

for (int i = 0; i < n; ++i) {
    outputs[i] = someValue; //should be equivalent to outputs[i].value = someValue
    outputs[i].error = lastValue - someValue;
}

Currently I just use outputs[i].value = but, for readability purposes, it actually would make more sense to use (something similar to) the above example (at least from a theory point of view, the code doesn’t require maintainability).

I understand that operator= would work for the above situation, but what about a simple access, I’d still have to use outputs[i].value. What would be the best solution to this for readability for both the conceptual design and also without causing headaches for the programmer.

  • 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-05-20T18:00:20+00:00Added an answer on May 20, 2026 at 6:00 pm

    You can add an assignment operator overload to data:

    class data {
    public:
        double error, value;
    
        void operator=(double d) { value = d; }
    };
    

    Though, to be honest, I think this would be rather confusing. It depends on how you intend to use it, of course, but given your example, I think it would be cleaner to add a constructor for the class:

    class data {
    public:
        double error, value;
        data(double value_arg, double error_arg)
            : value(value_arg), error(error_arg) { }
    };
    

    used as:

    outputs[i] = data(someValue, lastValue - someValue);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically at work I commonly run into code like: double pricediff = 0.0; if(!string.IsNullOrEmpty(someVariable.ToString()))
I commonly find myself extracting common behavior out of classes into helper/utility classes that
An idiom commonly used in OO languages like Python and Ruby is instantiating an
In the STL library some containers have iterators and it is commonly held that
I've run into a bit of a problem with a Regex I'm using for
We have are relatively simple Reporting Services report that our users commonly export to
Which Python library is commonly used today to generate Atom feeds? Note, there exists
I have a question about two operations you commonly see in an example Composite
Commonly when I look around the Internet, I find that people are generally using
Are there any commonly used patterns in Javascript for storing the URL's of endpoints

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.