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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:04:34+00:00 2026-05-26T11:04:34+00:00

Suppose that I have two sets of associated types, for example Animal s and

  • 0

Suppose that I have two sets of associated types, for example Animals and their Offspring:

/* Animal types */
struct Animal
{
  virtual string getType() const = 0;
};

struct Cat : public Animal
{
  virtual string getType() const { return "Cat"; }
};

struct Dog : public Animal
{
  virtual string getType() const { return "Dog"; }
};


/* Offspring types */
struct Offspring
{
  virtual string getType() const = 0;
};

struct Kitten : public Offspring
{
  virtual string getType() const { return "Kitten"; }
};

struct Puppy : public Offspring
{
  virtual string getType() const { return "Puppy"; }
};

I am trying to implement a factory which, given an Animal will return an object of the associated Offspring type (e.g. if the Animal is in fact a Dog, the factory will return a Puppy).

My first attempt at implementing such a factory looks like this:

// First attempt at OffspringFactory
class OffspringFactory1
{
  static Offspring* createKitten() { return new Kitten(); }
  static Offspring* createPuppy()  { return new Puppy();  }

public:
  // Create an Offspring according to the Animal type
  static Offspring* getOffspring(const Animal& a)
  {
    // Static mapping of Animal types to Offspring factory functions
    static map<string, Offspring* (*)()> factoryMap;
    if (factoryMap.empty())
    {
      factoryMap["Dog"] = &createPuppy;
      factoryMap["Cat"] = &createKitten;
    }

    // Lookup our Offspring factory function
    map<string, Offspring* (*)()>::const_iterator fnIt = factoryMap.find(a.getType());
    if (fnIt != factoryMap.end())
      return fnIt->second();
    else
      throw "Bad animal type";
  }
};

It works fine, but I’ve resorted to a string-based mapping rather than something purely type-based. In trying to move towards a more type-based implementation I arrived at this:

// Second attempt at OffspringFactory
class OffspringFactory2
{
  // Mapping Animal types to Offspring types
  template<typename TAnimal> struct OffspringMapper;

  template<>
  struct OffspringMapper<Cat> {
    typedef Kitten offspring_type;
  };

  template<>
  struct OffspringMapper<Dog> {
    typedef Puppy offspring_type;
  };

  // Factory method
  template<typename TAnimal>
    static Offspring* create() { return new OffspringMapper<TAnimal>::offspring_type(); }

public:
  // Create an Offspring according to the Animal type
  static Offspring* getOffspring(const Animal& a)
  {
    // Static mapping of Animal type strings to Offspring factory functions
    static map<string, Offspring* (*)()> factoryMap;
    if (factoryMap.empty())
    {
      factoryMap["Dog"] = &create<Dog>;
      factoryMap["Cat"] = &create<Cat>;
    }

    // Lookup our Offspring factory function
    map<string, Offspring* (*)()>::const_iterator fnIt = factoryMap.find(a.getType());
    if (fnIt != factoryMap.end())
      return fnIt->second();
    else
      throw "Bad animal type";
  }
};

Frankly, I’m not sure I’ve improved anything here: I still have my string mapping, along with quite a few more lines of less readable code…

Is there any merit in the second implementation over the first, and is there any way I can get rid of that map?

  • 1 1 Answer
  • 3 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-26T11:04:35+00:00Added an answer on May 26, 2026 at 11:04 am

    This looks like a classic case of double-dispatch. One pattern for solving this problem in C++ is the Visitor pattern.

    class Offspring;
    class OffspringFactory;
    
    class Animal {
    public:
        // ... rest of Animal class ...
    
        virtual Offspring* acceptOffspringFactory(OffspringFactory& factory)const = 0;
    };
    
    class OffspringFactory {
    public:
        Offspring* createCatOffspring()
        {
            return new Kitten;
        }
    
        // ... one createXOffspring() for each type of Animal
    
        Offspring* getOffspring(const Animal& a)
        {
            return a.acceptOffspringFactory(*this);
        }
    };
    
    Offspring* Cat::acceptOffspringFactory(OffspringFactory& factory)const
    {
        return factory.createCatOffspring();
    }
    
    // etc for rest of Animal classes
    

    Now that I look at your problem again, you don’t indicate that the factory is abstract, so really you could do away with the factory in entirety if you can add a method like @MooingDuck mentioned.

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

Sidebar

Related Questions

suppose you are given the following problem. You have two index sets that have
Suppose that I have two data types Foo and Bar. Foo has fields x
Suppose I have two types, TypeA and TypeB, that I want to register with
Suppose that I have two objects with the same property name that I am
Suppose I have two classes that both contain a static variable, XmlTag. The second
Suppose I have two tables that are linked (one has a foreign key to
Suppose I have two documents that are identical except the lines are shuffled. Is
Suppose I have two lists that holds the list of source file names and
Suppose I have two columns in a table that represents a graph, the first
Suppose i have one folder and inside that another two files are there .

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.