Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hbursk opened this issue Feb 9, 2019 · 0 comments

Comments

@hbursk
Copy link
Contributor

hbursk commented Feb 9, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant