Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to declare structures of data that should only be used in a certain context, C++
    text
    copied!<p>I'm having a rather general design problem and would like to solve it nicely. I'm writing remote control drivers in an embedded C++ project. There will be two types of remote control: joystick or radio. As I'd like the actual remote controller used to be transparent to an user-programmer, I'm also providing a base class for the two. So I'm going to have:</p> <pre><code>class RemoteControl {}; class JoystickControl : public RemoteControl {}; class RadioControl : public RemoteControl {}; </code></pre> <p>I'd like RemoteContol to just have one public method: <code>RemoteControl.getInput()</code>. This method should return a structure of data in common format, eg. <code>RCInput</code>, so the above method's declaration would be:</p> <pre><code>class RemoteContol { virtual RCInput getInput(); }; </code></pre> <p>Now, what's the best way to implement RCInput to be able to pass it to other classes' objects? I thought an inner class / structure might be a solution, but I never used it yet so can someone please provide an example of implementation and usage?</p> <p>Thanks in advance.</p> <p><strong>EDIT:</strong> What I did before was (could be references, this is not important at the moment):</p> <pre><code>typedef struct { int a; int b; } RCInput; class RemoteContol { public: virtual RCInput getInput() { return rcInput; } private: RCInput rcInput; }; </code></pre> <p>But I thought this way I'm allowing users to create their own structures of type RCInput also not related to RemoteControl, so I was looking for a better design. But maybe there isn't any.</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