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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:01:25+00:00 2026-05-27T03:01:25+00:00

Assume I have a Class Foo which has many internal variables, only one constructor

  • 0

Assume I have a Class Foo which has many internal variables, only one constructor and non modifying methods.
The calculation of the values for the internal variables involves state variables and complex functions and thus should not be part of Foo. Thus I created a class FooCreator that performs the calculus and in the end creates Foo.

First I implemented all member variables necessary for the creation of Foo separately in FooCreator and added a createFoo() function, but in the end FooCreator became almost a copy of Foo plus some more state variables.

Next I decided to let FooCreator inherit from Foo which saves a lot of typing but
this just does not feel like a clean solution, since to gain sth. I have to make Foo’s member variables protected instead of private, thus exposing more than I want for other users. Foo should not be used as base class except for the creation.

I had a look at the factory pattern but that seems overkill as well as the builder.
Surely this problem will be very common. So what would be the proper way to deal with this problem?

Some code example would look like this:

class Foo{
    private: //protected for second case
      int mVal;
      State mState;
      ComplexStuff mStuff;
      //...

    public:
      Foo():mVal(),mState(),mStuff(){}
      Foo(int val, State const& state, ComplexStuff const& stuff):
        mVal(val),mState(state),mStuff(stuff){}
      bool loadFromFile();

      bool evalStateTransition(State const& formerState) const{/*....*/}
      bool someTest(int) const{/*....*/};
      GraphicItem* visualize() const{/* ....*/};
      //...  
  };

  class FooCreator{  
    private:
      int mVal;
      State mState;
      ComplexStuff mStuff;

      StuffVisualizer mStuffVis;
      Paramset mParams;    
      //...

    public:
      FooCreator(Paramset const& set):
        mVal(),mState(),mStuff(),mStuffVis(),mParams(set){}
      constructState(int, int, int);
      gatherStuff1(File file1);
      Foo createFoo();

      int evalStateTransition(State const& formerState)  const{
        /*same long code as in Foo*/
      }
      bool someTest(int) const{ /*same long code as in Foo*/}
      GraphicItem* visualize() const{ /*same long code as in Foo*/}
      //...  
  };

  class FooCreator2 : public Foo{  
    private:
      StuffVisualizer mStuffVis;
      Paramset mParams;    
      //...

    public:
      FooCreator(Paramset const& set):Foo(),mParams(set){}
      constructState(int, int, int);
      gatherStuff1(File file1);
      Foo createFoo();    
  };
  • 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-27T03:01:26+00:00Added an answer on May 27, 2026 at 3:01 am

    just write friend class FooCreator; in Foo, and now you can access all the private fields of Foo from FooCreator. Then just create a field of type Foo in FooCreator… Nice, and easy.

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

Sidebar

Related Questions

Assume I have a class foo, and wish to use a std::map to store
Let's assume we have a template function foo: template<class T> void foo(T arg) {
Assume you have a list item, <li id=foo> which you want to fade from
I have a class Foo which overrides equals() and hashCode() properly. I would like
If I have an abstract class in java named Foo and it has an
I have the following code which has a mutable Person class, String and a
Assume I have this: class foo{ Member member; foo(); ~foo(); }; How I should
Assume I have such simple model: class Foo(models.Model): name = models.CharField(max_length=25) b_date = models.DateField()
Assume I have an instance foo of some class Class with some variable foo.x.
Assume that I have the follow class. class Foo # I want the following

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.