Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Accept Any Struct In Parameter
    text
    copied!<p>I'm new in C++ and I would like to know if we can accept any struct as our method parameter.</p> <p>This is the case: I have one class (let's say <code>hw_manager</code>) that interact with the hardware class (let's say <code>hw_device1</code>). Currently <code>hw_manager</code> will call the method of <code>hw_device1</code> and the result of the method will be returned via the struct parameter (send the struct parameter as reference, and change the value of the referenced parameter).</p> <p>In C++ code should be like this:</p> <pre><code>struct sSensorStatus { unsigned char sensor1; unsigned char sensor2; }; bool checkSensorStatus(struct sSensorStatus &amp;status) { // Change the status here } </code></pre> <p>Now, since the hardware is changed, I need to create a new class, let's say <code>hw_device2</code> which has totally different operation. </p> <pre><code>struct sHardwareStatus { unsigned char loader; unsigned char transport; unsigned char ejector; unsigned char mouth; }; bool checkHardwareStatus(struct sHardwareStatus &amp;status) { // Change the status here } </code></pre> <p>Rather than changing the code in <code>hw_manager</code> (that will affect the code above this layer) I'm planning to implement an interface, let's say <code>IHardware</code> that has <code>doAction</code> method. The idea is like this:</p> <pre><code>bool doAction(int cmdID, ????) { // switch case cmdID // based on the cmdID, type cast the ???? into the struct } </code></pre> <p>What should I put in <strong>????</strong> to accept any kind of struct? Can I do this in C++?</p> <p>Thanks</p> <p><strong>EDIT</strong></p> <p>Inside the hardware, I will also have another struct, so I don't think using template will be appropriate. Sorry for late clarification.</p>
 

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