user54202: I have a function that accept an HardwareSerial class as argument:
void ProcessLine(HardwareSerial *serial, char *line)
{
    // do something
    serial->write("blablabla");
}

In my code I also have a BluetoothSerial that shares the required functions (i.e. write()) with HardwareSerial.
To avoid to duplicate the code of my ProcessLine() that is quite long, is there a way to "cast" a BlutoothSerial to a HardwareSerial?

user4143: No, you don't "cast" between them. Instead you make use of polymorphism.
In the Arduino framework most classes fit into a "tree" with similar classes "hanging off" a common parent "branch".  In this case both BluetoothSerial and HardwareSerial, being both serial classes, are both children of the Stream class.  Indeed it is this Stream class (which itself is a child of the Print class) which gives these objects most of their functions.
So unless you need access to some function that is specific to the HardwareSerial class (such as the .begin(...) function) you should always work up the tree to the class that provides the most restrictive set of functions that suit your needs. In this case, since you are only printing data, it would be the Print class.  If you were reading data you would choose the Stream class.
So if you change your function to accept Print *serial instead of HardwareSerial *serial then it will accept any object that ultimately inherits from the Print object - so any serial objects, any network stream objects, etc.

54202: <p>I have a function that accept an <code>HardwareSerial</code> class as argument:</p>
<pre><code>void ProcessLine(HardwareSerial *serial, char *line)
{
    // do something
    serial-&gt;write(&quot;blablabla&quot;);
}
</code></pre>
<p>In my code I also have a <code>BluetoothSerial</code> that shares the required functions (i.e. <code>write()</code>) with <code>HardwareSerial</code>.</p>
<p>To avoid to duplicate the code of my <code>ProcessLine()</code> that is quite long, is there a way to &quot;cast&quot; a <code>BlutoothSerial</code> to a <code>HardwareSerial</code>?</p>
 4143: <p>No, you don't &quot;cast&quot; between them. Instead you make use of <em>polymorphism</em>.</p>
<p>In the Arduino framework most classes fit into a &quot;tree&quot; with similar classes &quot;hanging off&quot; a common parent &quot;branch&quot;.  In this case both <code>BluetoothSerial</code> and <code>HardwareSerial</code>, being both serial classes, are both children of the <code>Stream</code> class.  Indeed it is this <code>Stream</code> class (which itself is a child of the <code>Print</code> class) which gives these objects most of their functions.</p>
<p>So unless you <em>need</em> access to some function that is specific to the <code>HardwareSerial</code> class (such as the <code>.begin(...)</code> function) you should always work up the tree to the class that provides the most restrictive set of functions that suit your needs. In this case, since you are only printing data, it would be the <code>Print</code> class.  If you were reading data you would choose the <code>Stream</code> class.</p>
<p>So if you change your function to accept <code>Print *serial</code> instead of <code>HardwareSerial *serial</code> then it will accept <em>any</em> object that ultimately inherits from the <code>Print</code> object - so any serial objects, any network stream objects, etc.</p>
