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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:21:54+00:00 2026-06-05T15:21:54+00:00

I am trying to develop a game and I was having issues managing the

  • 0

I am trying to develop a game and I was having issues managing the creation and destruction of game objects and was suggested by several people to try using a factory pattern.
I went and read up on the factory pattern and I am trying to implement it and I have hit a roadblock.

// inside of EnemyGuy.h
#pragma once
#include "Entity.h"
#include "EntityFactory.h"
class EnemyGuy: public Entity {
public:
    void update();
}

//inside of EnemyGuy.cpp
#include "EnemyGuy.h"
void EnemyGuy::update(){
    if (you see player)
        Entity* shotFired = EntityFactory::create("bullet", params);
}

// inside of EntityFactory.h
#pragma once
class Entity
#include "Bullet.h"
#include "EnemyGuy.h"
class EntityFactory{
public:
    static Entity* create(const std::string passed, params);
}

// inside of EntityFactory.cpp
#include "EntityFactory.h"
 static Entity* create(const std::string passed, params){
    if (passed == "bullet")
        return new Bullet(params);
    if (passed == "enemyguy")
        return new EnemyGuy(params);
 }

I get a cyclic dependency error because the factory needs to include EnemyGuy so it can create instances of it and then EnemyGuy needs to include the factory so it can call the create() method.

Normally you would break a cyclic dependency with a forward declare but in this case a forward declare won’t do it.
How can I resolve this issue?

  • 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-05T15:21:55+00:00Added an answer on June 5, 2026 at 3:21 pm

    The factory header file doesn’t need to include it. Separate the declaration and the implementation and you’ll be fine.

    EntityFactory.h:

    class Entity
    
    class EntityFactory{ public:
         static Entity* create(const std::string passed, params);
    }
    

    EntityFactory.cpp:

    #include "EntityFactory.h"
    #include "Bullet.h"
    #include "EnemyGuy.h"
    
     Entity* EntityFactory::create(const std::string passed, params){
         if (passed == "bullet")
             return new Bullet(params);
         if (passed == "enemyguy")
             return new EnemyGuy(params);
     }
    

    The idea is that the factory header file and declaration never change, regardless of how many new options you add to it.

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

Sidebar

Related Questions

trying to develop web form using jquery. all i need is to have several
i'm trying to develop Pentago -game in c#. right now i'm having 2 players
While trying to develop my first VS Addin, I am having issues in firing
I trying to develop a game for iPhone by using Cocos2d. I need 'DRAG'
I'm trying to develop an Android 2D game using OpenGL ES that uses a
I'm trying to develop a simple 2D fighting game using OpenGL, and I'm stuck
I am trying to develop a Web-Client based Online game using GWT. The game
Hello StackOverflow Users, I am new to android and trying to develop a game
I am trying to develop a simple game where a group of user can
I'm trying to develop my first XNA game by my own hands, just looking

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.