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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:44:07+00:00 2026-06-07T02:44:07+00:00

I have a configuration class that I want to pass to another class (called

  • 0

I have a configuration class that I want to pass to another class (called Use) in a constructor. I want to store the configuration class inside of the Use class as a private member variable. I would like it to be const.

So far, I have this code:

class Configuration{
private:
    int value1_;
public:
    Configuration();
    Configuration(int value1){value1_=value1;}
    int value1() const {
        return value1_;
    }
};

class Use{
private:
    //const me
    Configuration config_;
    int something_;
public:
    Use(Configuration &config){
        config_=config;
    }

    void doSomething(){
        something_+=config_.value1();
    }
};

I want to const Use::config_, but every way that I try ends in confusing compile errors. How do I do it?

  • 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-07T02:44:09+00:00Added an answer on June 7, 2026 at 2:44 am

    The best way to support this is to use a reference

    class Use{
    private:
        const Configuration& config_;
        int something_;
    public:
        Use(const Configuration &config) : 
          config_(config), 
          something_(0) {
    
        }
    
        void doSomething(){
            something_+=config_.value1();
        }
    };
    

    One item that needs to be a bit clearer in the question is the relationship between Configuration and Use instances. If there is one Configuration which is meant to be shared amongst all Use instances then a reference (as I outlined) is the most appropriate choice. In that scenario though you need to ensure that Configuration outlives all Use instances that take a reference to it.

    On the other hand if Configuration is meant to be one per Use instance then a non-reference solution is probably more appropriate

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

Sidebar

Related Questions

I have a class that I want to use in many test cases: require
I have a State class that includes configuration as well as some state-specific methods.
Let's say I have a class that requires configuration, dependency injection etc. public class
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
People says that libraries shouldn't have configuration files. I can't pass arguments to my
Suppose you have a class Foo with private member of type Bar. You don't
I have a dynamic class that serves as a storage container for configuration settings.
I have an abstract class that I use to set default values in a
I have a class on which I want to allow several (~20+) configuration options.
I have a class that looks like following. Class Parser{ private String fileName; Parser(String

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.