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

  • Home
  • SEARCH
  • 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 9061687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:32:58+00:00 2026-06-16T15:32:58+00:00

currently I switch from Java to C++ and this is giving me a hard

  • 0

currently I switch from Java to C++ and this is giving me a hard time (but lots of new experience ^^). I’m writing some data-transfer-objects which contain configurations for my program. I’ve written some classes and now I want to have a class which behaves like a container.

Here’s a header for the container:

class MyContainer{

public:
MyContainer();
virtual ~MyContainer();

Config getConfig(TypeEnum type) {
    switch (type) {
    case ATYPE:
        return config_a;
    case BTYPE:
        return config_b;
    default:
        break;
    }

}

ConfigA config_a;
    ConfigB config_b;
};

The configs have some data in it and are derived from another config-file.

And here is the C++-Source:

 MyContainer::MyContainer(){
  ConfigA config_a(int c = 1);
  ConfigB config_b(double b = 2.1);
  this->config_a = config_a;
  this->config_b = config_b;
}

There are several problems I think. But the main question for me is:
How can I get those configs in this container to share it to other modules of my program? I have tried to make config_a to a pointer but I always get error-messages that these types won’t match.

 this->config_a = &config_a; //If ConfigA config_a; was ConfigA *config_a; instead

If you have another minute for me then please tell me if the getConfig-Method could work like this.

And if there’s another topic for this then please share. Thanks.

  • 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-16T15:32:59+00:00Added an answer on June 16, 2026 at 3:32 pm
    1. If you write ConfigA configA in your header, the configA is automatically allocated during the allocation of the container class. So you don’t have to initialize it like following:

      ConfigA config_a(1);
      this->config_a = config_a;
      

      Instead, you can just do the following:

      this->config_a->value = 1;
      
    2. There is no need to write:

      ConfigA config_a(int c = 1);
      

      In short words, the mentioned int c = 1 is an operation, which:

      • allocates space on heap for temporary variable c (this may be done when entering the method as well)
      • assigns value to it, which has side effect of returning the right-hand-side value
      • the returned right-hand-side value is applied to the ConfigA constructor.

      To understand this, try the following:

      int a, b, c;
      c = (a = 2) + (b = 8);
      printf("a = %d, b = %d, c = %d\n", a, b, c);
      
    3. If you want to pass the configs to another modules, you can choose one of the following solutions:

      a) Accept the configs as references (the config classes have to derive from the same base class):

      ConfigA & configA = dynamic_cast<ConfigA &>(container.get_config(ATYPE));
      

      In this case, container shall return the configs in the following way:

      return this->configA;
      

      But the header shall be modified:

      Config & getConfig(TypeEnum type) { (...)
      

      b) Accept the configs as pointers (same as above)

      ConfigA * configA = dynamic_cast<ConfigA *>(container.get_config(ATYPE));
      

      In this case, container shall return the configs in the following way:

      return &(this->configA);
      

    Finally, this is the way I would do it:

    class ConfigA
    {
    private:
        int i;
    
    public:
        int GetI() const { return i; }
        void SetI(int newI) { i = newI; }
    };
    
    class ConfigB
    {
    private:
        float f;
    
    public:
        float GetF() const { return f; }
        void SetF(float newF) { f = newF; }
    };
    
    class Config
    {
    private:
        ConfigA configA;
        ConfigB configB;
    
    public:
        ConfigA & GetConfigA() { return configA; }
        ConfigB & GetConfigB() { return configB; }
    };
    
    class AppContext
    {
    private:
        Config config;
    
    public:
        Config & GetConfig() { return config; }
    };
    
    // Somewhere in the code
    
    Config & config = context.GetConfig();
    ConfigA & configA = config.GetConfigA();
    configA.SetI(44);
    

    The const-correctness is also worth mentioning, but we’ll leave it as an exercise for the OP 😉

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

Sidebar

Related Questions

I'm currently switch from ubuntu 11.04 (gcc 4.5) to ubuntu 12.04 (but I have
i currently have a Java Observer/Observable setup in which i switch on some field
This was working correctly in Visual Studio 2008, but since I have switch to
I'm currently trying to switch from Coda (a Mac IDE) to Vim. One thing
I'm attempting to switch from Vim to Emacs, but I'm tearing my hair out
Apple's Java update this week removes the Java Preferences.app from Utilities. When working between
I am currently developing a blackberry java application. In the app, I switch between
I'm currently using IntelliJ IDEA for Java development, but I'm also interested in answers
I'm a novice programmer with basic Java experience, and currently learning Python. I've stumbled
Currently I'm building my Java Web Application on Google AppEngine (GAE), but due to

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.