Trying to extend HardwareSerial, only two bytes are sent to Serial Monitor

I'm trying to extend HardwareSerial to add some more features to it.

The code seems to extend fine, but only first two bytes are sent to the output.

I tried both print() and write(), searching on google and banging my head against the wall for hours but didn't helped.

Any help would be appreciated.

Used hardware: Arduino Mega 2560

class MySerial : public HardwareSerial
{
  public:
    MySerial() : HardwareSerial(Serial){
    }
    
    size_t helloWorld() {
        return print("HelloWorld!");
    }
};

MySerial mySerial;


void setup() {
  mySerial.begin(9600);
}

void loop() {
  mySerial.helloWorld();
}

I think you are going to struggle with that. It is not as simple as extending the HardwareSerial class since other parts of the implementation occur outside the class itself, specifically, setting up the ISRs.
By invoking HardwareSerial's implicitly defined copy constructor in MySerial's constructor you have gotten your own copy of Serial's internal data structure. When you transmit something, mySerial's data structure is initially used, but incoming interrupts keep using Serial's data structure.

What features do you want to add to HardwareSerial that can't be done by a class which does the needful and then delegates the actual serial part to an existing HardwareSerial instance?

Thanks Arduarn. Do you know how to fix it?

I need to overload print() and write(). It's possible to add a separate class, but I want it to be clean and consistent across the whole application.

I need to overload print() and write().

What are you doing that needs Serial changing ?

eddy_nuh:
Do you know how to fix it?
... I want it to be clean and consistent across the whole application.

I've given it some thought and no, I can't suggest anything that you can do in a sketch that wouldn't be a hack and be worse than any perceived lack of cleanliness or consistency that you currently believe you are having to suffer under.

The Arduino platform is designed to make things fairly simple for beginners. Sometimes that makes more advanced techniques difficult or effectively impossible. Just gotta live with it or write your own platform abstraction layer.