How do I implement the factory pattern in game engines with C++?

The Factory Pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. In game engines, the Factory Pattern can be used to streamline the creation of game objects, making the code more maintainable and scalable.
Factory Pattern, Game Engines, C++, Design Patterns, Object Creation
        #include <iostream>
        #include <memory>
        #include <string>

        // Base class
        class GameObject {
        public:
            virtual void update() = 0;
            virtual ~GameObject() {}
        };

        // Derived classes
        class Player : public GameObject {
        public:
            void update() override {
                std::cout << "Updating Player" << std::endl;
            }
        };

        class Enemy : public GameObject {
        public:
            void update() override {
                std::cout << "Updating Enemy" << std::endl;
            }
        };

        // Factory class
        class GameObjectFactory {
        public:
            static std::unique_ptr<GameObject> createGameObject(const std::string &type) {
                if (type == "Player") {
                    return std::make_unique<Player>();
                } else if (type == "Enemy") {
                    return std::make_unique<Enemy>();
                }
                return nullptr;
            }
        };

        // Client code
        int main() {
            auto player = GameObjectFactory::createGameObject("Player");
            auto enemy = GameObjectFactory::createGameObject("Enemy");

            player->update();
            enemy->update();

            return 0;
        }
        

Factory Pattern Game Engines C++ Design Patterns Object Creation