The noteOn function is a static member of the MidiMessage class, which means you can call it from outside any particular instance. It will generate for you an object, which you can use as above, or have a line like…
MidiMessage m (MidiMessage::noteOn(1,50,127));
… to create one in scope to be used for multiple tasks. You’d then of course use