Skip to content

README is unclear on how to initialize member variables of multi level subtypes #2

@hbursk

Description

@hbursk

README states:


// get the root type from the hierarchy which serves as reference/pointer to base class.
using Event = ni::type_hierarchy::from_base<EventBase>;

// derive custom types, the first template argument must always be the derived type
// itself. This is required due to the missing reflections in C++ and is used to
// assign a unique id to the new type.
struct MouseEvent : ni::sub_type<MouseEvent, Event> {
    int x, y;
    MouseEvent(int x_, int y_) : x{x_}, y{y_} {}
};
struct KeyEvent : ni::sub_type<KeyEvent, Event> {
    // ...
};

// 2nd level event
struct MouseButtonDownEvent : ni::sub_type<MouseButtonDownEvent, MouseEvent> {
    // ...
};

It does not specify how to declare the constructor for MouseButtonDownEvent, or how to initialize the int x,y, parent members or how to declare and initialize the child members.

From experimenting, using the standard inheritance approach of:

struct MouseButtonDownEvent : ni::sub_type<MouseButtonDownEvent, MouseEvent> 
{
       bool button;
       MouseButtonDownEvent(int x_, int y_, bool button_ ) : MouseEvent(x_, y_), button(button_) {}

does not compile. Instead I've had to do:

struct MouseButtonDownEvent : ni::sub_type<MouseButtonDownEvent, MouseEvent> 
{
       bool button;
       MouseButtonDownEvent(int x_, int y_, bool button_ )
      {
             x = x_; 
             y = y_;
             button = button_;
      }
};

I'm not sure if this is intended method of initialization. More detail in the README would be very helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions