Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Stealth" class to enclose a variable in C++
    primarykey
    data
    text
    <p>I'm currently using two identical libraries for parsing commands received in Arduino (via <a href="http://en.wikipedia.org/wiki/Internet_protocol_suite" rel="nofollow">TCP/IP</a> and via Serial). All the difference is in the object type being passed to the parser.</p> <pre><code>//main.cpp SerCmd sCmd; EthCmd eCmd; void setup() { sCmd.listen("#",dumpCmd); eCmd.listen("#",dumpCmd); } void loop() { HardwareSerial SerClient = Serial.available(); EthernetClient EthClient = Server.available(); eCmd.read(SerClient); sCmd.read(EthClient); } //SerCmd.h class SerCmd { public: void read(HardwareSerial &amp;dataObj); } //EthCmd.h class EthCmd { public: void read(EthernetClient &amp;dataObj); } </code></pre> <p>It's obvious that:</p> <ul> <li>The two objects are nearly identical, and they share lots of common methods (sCmd, eCd).</li> <li>The two classes (SerCmd, EthCmd) can be replaced with a single class to reduce the <a href="http://www.arduino.cc/en/Tutorial/Sketch" rel="nofollow">sketch</a> size.</li> </ul> <p>How can I create a "stealth" class which could be passed to the <code>read()</code> method, while still addressing any of the two different objects underneath?</p> <p>What's the common practice used in such situations?</p> <p>So far, I am able to compile the below code, but cannot initialize any methods:</p> <pre><code>class Communication {}; HardwareSerial *SerClient; Communication *Comm; void setup() { Comm = (Communication*) SerClient; //Comm.begin(9600); //Error: request for member 'print' in 'Comm', //which is of non-class type 'Communication*' } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload