Easiest to understand way using a struct-ured variable with functions

Hi everybody,

If I write code I highly prefer easy to understand code over most compact, most elegant or fastest.

For my USB-keyboard-emulator-project I intend to write a library for the german keyboard-layout.

The interfacing functions shall work as a drop-in replacement like the original keyboard-library.

for internally used functions I want to rewrite the code in a way that makes the code easier to understand for newcomers.

Under this aspect the ideally code would work this way

Sending keystrokes over USB needs to setup a structured variable like this

typedef struct
  uint8_t modifiers;
  uint8_t reserved;
  uint8_t keys[6];
} KeyReport;

The input to this transformation is a single character like "a", "b" "@", "K" or "[" etc.

the output is a correct set of key and modifier example for "@"
key = 'q';
modifier = Ctrl + Alt;

as a generalised example

typedef struct
  int myIntegerVar;
  long  myLongVar;
  char myShortName[6];
} myDataSet_t;

in programming-languages like Pascal/Delphi (where I'm coming from)

functions could return any datatype even the most nested version of structs of strucs of arrays

If I understand right in C++ the following definition of a function is



myDataSet_t  myFunc(byte myInputVar) {
   myDataSet_t myLocalDataSet ;

  myLocalDataSet.myIntegerVar = myInputVar * 100;
  myLocalDataSet.myLongVar = myInputVar * 50000;
  myLocalDataSet.myShortName = "XY1234"; 

  return myLocalDataSet; 

myDataSet_t myDataSetVar;

myDataSetVar = myFunc(12);

in Delphi you would code it this way with no problems at all.
assigning a return-"value" (or in this case a structured variable)
to a variable of the same type.

In C++ it has to be coded in a different way.
What would be a way that - for newcomers - is easy to understand?

I want to exclude pointers as "easy to understand"

Of course you could code assign all values to a global defined variable
This would require an own function for every globally defined variable.

next best thing would be to pass the structured variable as a parameter

void myFunc(myDataSet_t   &myParameter, byte myInputVar) {
  myParameter.myIntegerVar = myInputVar * 100;
  myParameter .myLongVar = myInputVar * 50000;
  myParameter .myShortName = "XY1234"; 

not possible too (if I understand C++ right so far)

So which way would come close to this version

myDataSet_t mySecondDataSetVar;

mySecondDataSetVar = myFunc(12);


is easy to understand for newcomers?
best regards Stefan

That will work. C++ can return a local struct variable. In this case, it will be done with a simple shallow copy. A struct with dynamically-allocated memory (an Arduino String variable for example) would require a proper copy (or move) constructor.

The (more efficient) alternative is to pass the struct variable FROM the calling function INTO myFunc() by using a C++ reference. myFunct() would then operate on that variable directly even though it "belongs" to the calling function.

The latter of these seems better to me.

A C++ reference '&' means a var parameter in Delphi.

C++ also provides for constant reference parameters. For example:

void aFunction(const myDataSet_t &variableName) {

This provides the efficiency of a reference while forbidding the function from changing the parameter. I don't know if there's an equivalent construct in Pascal/Delphi.

Of course, the features of references and constant references could also be achieved by using pointers.

So does Delphi. The compiler is free to translate it into a value or pointer, whatever is shorter or faster.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.