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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:03:22+00:00 2026-06-09T11:03:22+00:00

I need to construct two objects, A and B . These A and B

  • 0

I need to construct two objects, A and B. These A and B objects can be initialized in different ways.

Design pattern to construct A (working):

A has subclasses A_txt, A_tsv, A_csv, etc. Each A subtype is constructed from a string (a file path), but each one loads the file in a different way. I’ve built a registry that maps the file type (e.g. txt) to the appropriate factory for creating that object:

txt –> A_txt_Factory
tsv –> A_tsv_Factory
…

Where A_txt_factory receives a file path, loads it as a text file, and then returns a pointer to an A_txt object.

Together, I use this registry as a factory: given a file path and a file type, it looks up the factory corresponding to that tag and uses it to construct and returns a pointer to an A object.

I really like this combination registry and factory design pattern; someone else can easily make their own file type and a factory for loading it, check it into the registry, and use it without looking at the other code.

Design pattern to construct B (I need help):

Constructing B objects is more difficult, because they don’t all load from the same type (e.g. all A subtypes are constructed with a string indicating the file path). B_text will load from a text file (from a provided string indicating the file path), but unlike before B_intAndA may initialize from an int and an instance of A.

What design pattern can I use to create B? As before, I will have a string tag to indicate which factory will be used; however, unlike before, each factory will accept different parameters (and possibly a different number of parameters). I suppose that could be accomplished somehow with a variadic template function of variadic parameters (I’m using C++11, so that is an option).

Does this design pattern ever come up for anyone else (same base class, but constructed with different numbers of arguments with different types)?

It’s easy to make individual factories that construct the different B subtypes, but difficult to know how to allow them to be indexed with a registry since they accept different types and numbers of parameters.

How would you handle this?

  • 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-09T11:03:25+00:00Added an answer on June 9, 2026 at 11:03 am

    At some time point T1 the kind of B to be created, is known. The kind of B implies the signature of the constructor. At some time point T2, which might be the same or later, the B is created.

    If the argument values are known at point T1, then if T2 = T1 just create the B here and problem solved. Otherwise if T2 > T1, store the arguments and the creation call in a functor (e.g. [=]() -> B* { return new B_int_and_A( 42, *anAPointer ); }) and pass that, e.g. as a std::function, to T2.

    Otherwise, if argument values are not known at point T1 then you must have T2 > T1, and the argument values – regardless of kind of B – must be known at T2. In this case pass a B kind id forward to T2. Then at point T2 look up the arguments and creation function from the id, for example in a switch.

    I don’t see how this has anything to do with C++, though.

    What’s the connection?

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

Sidebar

Related Questions

I'm currently working on a class that calculates the difference between two objects. I'm
I need to copy two objects (which are both stacks). I want to copy
I need to construct a link in Wordpress based on a variable out of
I need to construct the URL of a page in a String, to send
I need to construct a long string with javascript. Thats how i tried to
I need to construct an if statement from the data coming from the client
I need to construct a file path inside a java program. Which path separator
I need to construct directed graph (at runtime) with cycles in Prolog and I
Currently i need to construct and update large xml files. So what is the
Suppose I need to construct a tuple of length three: (x , y, z)

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.