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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:04:01+00:00 2026-05-15T21:04:01+00:00

Consider a class like this one: class MyReferenceClass { public: MyReferenceClass(); const double ImportantConstant1;

  • 0

Consider a class like this one:

class MyReferenceClass
{
public:
    MyReferenceClass();
    const double ImportantConstant1;
    const double ImportantConstant2;
    const double ImportantConstant3;
private:
    void ComputeImportantConstants(double *out_const1, double *out_const2, double *out_const3);
}

There is a routine (ComputeImportantConstants) that computes three constants at runtime. Suppose the computation is fairly complex, and inherently produces all three values at once. Moreover, the results depend on build configuration, so hardcoding the results isn’t an option.

Is there a sensible way to store these computed values in the corresponding const double fields of the class?

If not, can you suggest a more natural way to declare such a class in C++?

In C# I would use a static class with a static constructor here, but that isn’t an option in C++. I have also considered making ImportantConstant1..3 either non-const fields or function calls, but both seem inferior.

The only way to initialize const fields that I found is to use initializer lists, but it doesn’t seem possible to pass the results of a multi-output computation in such a list.

  • 1 1 Answer
  • 1 View
  • 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-15T21:04:01+00:00Added an answer on May 15, 2026 at 9:04 pm

    Why can’t you do:

    MyReferenceClass ComputeImportantConstants(){
        //stuff to compute
        return MyReferenceClass( const1, const2, const3 );
    }
    
    MyReferenceClass{
    public:
        MyReferenceClass(double _1, double _2, double _3) 
            : m_Const1(_1),
            m_Const2(_2),
            m_Const3(_3){}
    
        double getImportantConst1() const { return m_Const1; }
        double getImportantConst2() const { return m_Const2; }
        double getImportantConst3() const { return m_Const3; }
    private:
        const double m_Const1,
                     m_Const2,
                     m_Const3;
    };
    

    Like that and have the calculate function turn into a factory function?

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

Sidebar

Related Questions

Consider this code: class DatabaseCommands { @Privilege(ADMIN) public void delete(Something thingToDelete, User currentUser) {
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Consider: public class HomeController : Controller { private IDependency dependency; public HomeController(IDependency dependency) {
Consider this class: class test { public function __set($n, $v) { echo __set() called\n;
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private
Consider a following chunk of service: public class ProductService : IProductService { private IProductRepository
Consider I have a Task class like this : class Task { String title
Please let me know which one is good Consider I have a class like
Consider this class: class foo(object): pass The default string representation looks something like this:
Consider the following code (C# 4.0): public class Foo : LambdaExpression { } This

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.