Is there a way to refer to child classes with the parent classes namespace/classspace?

Viewed 75

Suppose I have the following Mouse Event class with a Button event and extending Button Press and Button Release events:

namespace Event {
    namespace Mouse {
        class Button : public Event {
          public:
            inline Input::Code::Mouse get_mouse_button() const {
                return m_Button;
            }
    
            EVENT_CLASS_CATEGORY(Event_Category_Input | Event_Category_Mouse)
          protected:
            Button(Input::Code::Mouse button)
                : m_Button(button) {}
            Input::Code::Mouse m_Button;
        };
    
        class Press : public Button {
          public:
            Press(Input::Code::Mouse button) : Button(button) {}
    
            std::string to_string() const override {
                return fmt::format("Mouse Button Press: {0}", m_Button);
            }
    
            EVENT_CLASS_TYPE(Mouse_Button_Press)
        };
    
        class Release : public Button {
          public:
            Release(Input::Code::Mouse button)
                : Button(button) {}
    
            std::string to_string() const override {
                return fmt::format("Mouse Button Release: ", m_Button);
            }
    
            EVENT_CLASS_TYPE(Mouse_Button_Release)
        };
    }
}

I would like to refer to these with Event::Mouse::Button::Press and Event::Mouse::Button::Release instead of Event::Mouse::Press and Event::Mouse::Release. I cannot directly add the namespace Button to force this behavior because it would conflict with the Button class which is already defined. Is there a way to accomplish this? I understand I could name it Button_Press and Button_Release respectively, but I'd prefer not to for stylistic reasons. Thanks in advance! If it's not possible, I'll just suck it up and use the underscores.

2 Answers

Since you want Press and Release to be part of Event::Mouse::Button in name, I would make them part of Event::Mouse::Button in definition. The only trick here is that the definitions of Press and Release cannot be inside the definition of Button (but their declarations must still be there).

namespace Event {
    namespace Mouse {
        class Button : public Event {
            class Press;    // <-- declare here
            class Release;
        };
        // End of class Button
    
        // Begin definition of Button::Press
        class Button::Press : public Button {
        };
    
        // Begin definition of Button::Release
        class Button::Release : public Button {
        };
    }
}

(Please note how I removed unnecessary and distracting clutter from your example. This also makes the example more complete, lacking only a definition of the Event class that Button extends.)

Yes, you can do this.

Forward declare these types in your Button class.

That gives the types the scope you want.

class Button : public Event {
  public:

    class Press;
    class Release;        

};

Then, outside the class you may define them.

You must define them outside the class, so the parent class Button is completely defined.

class Button::Press : public Button {
public:

};

class Button::Release : public Button {
public:

};
Related