lundi 3 juillet 2023

C cannot cast to parent when declared inside class [closed]

So I have an architecture where 2 classes depend on eachother so it's best if I create one of the classes inside another class. However I have a piece of code where I add that class defined inside the other class to a vector, which should cast it to it's parent.

Here is the parent class:

class ENGINE_EXPORT Layer {
        public:
            Layer(const std::string& name = "Layer");

            virtual ~Layer();

            virtual void OnAttach() = 0;
            virtual void OnDetach() = 0;
            virtual void OnUpdate() = 0;
            virtual void OnEvent(Events::Event& event) {};

            inline const std::string& GetName() { return m_DebugName; };

        private:
            std::string m_DebugName;

        };

Here are the intertwined classes:

//in the hpp file:
class Parent{
...
private:
   class Window;
   std::unique_ptr<Window> pImpl;
};
// in cpp file:
class Parent::Window{
        class CameraLayer : public Graphic::Layer
        {
        public:

            CameraLayer(Window* ref) : Layer("CameraLayer")
            {
                this->ref = ref;
            };

            void OnAttach() override {};
            void OnDetach() override {};
            void OnUpdate() override {};
            void OnEvent(Events::Event& event) override
            {
                using namespace Events;

                EventDispatcher dispatcher(event);

                switch (event.GetEventType())
                {
                case EventType::KeyPressed:
                    dispatcher.Dispatch<KeyPressedEvent>([this](Events::Event& event) -> bool {
                        if (auto* keyPressedEvent = dynamic_cast<Events::KeyPressedEvent*>(&event))
                        {
                            ENGINE_INFO("Handle Key Pressed Event");
                            ref->m_Camera->ProcessKeyboard((Camera_Movement)keyPressedEvent->GetKeyCode(), 0.10);
                            return true;
                        }
                        });
                    break;
                };
            }
        private:
            Window* ref;
        };

public:
    Window(): camera_layer(new CameraLayer(this))
    {  
       ...
       this->layer_stack->push(camera_layer);
    }

...
private:
    CameraLayer* camera_layer;

};

The thing is, I can't move CameraLayer out of Window because then it says Window is undefined. I can't move it under the Windows definition because then I can't forward declare inheritance, preventing me to cast it to layer stack even further.

Also note that Window is actually a pImpl so it itself is a private member of it's parent class and I don't want to expose CameraLayer.

How can I get over this situation?

Aucun commentaire:

Enregistrer un commentaire