Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ message lookup table (iostream)
    text
    copied!<p>Is there an easy way in C++ to have a message lookup table that allows the use of variables. </p> <p>For example in C you can have something like this:</p> <pre><code>const char* transaction_messages[] = { "UNAUTHORIZED", "UNKNOWN_ORDER", "UNKNOWN_DEALER", "UNKNOWN_COMMODITY", "INVALID_MESSAGE", "BOUGHT %d %s @ %f.1 FROM %s", //amount, commodity, price, dealer "SOLD %d %s @ %f.1 FROM %s", //amount, commodity, price, dealer "%d HAS BEEN FILLED", //order id "%d HAS BEEN REVOKED", //order id "%d %s %s %s %d %f.1 HAS BEEN POSTED", //order id, dealer, side, commodity, amount, price "%d %s %s %s %d %f.1" //order id, dealer, side, commodity, amount, price }; </code></pre> <p>and then use it in a function like thislike this:</p> <pre><code>void myfunction(int amount, char* commodity, double price, char* dealer){ char *msg = transaction_message[6]; printf(msg, amount, commodity, price, dealer); } </code></pre> <p>I want to be able to do the same with an ostream instead of having to do something with the &lt;&lt; operator where the same thing would look like:</p> <pre><code>ostream &lt;&lt; "BOUGHT" &lt;&lt; amount &lt;&lt; " " &lt;&lt; commodity &lt;&lt; " @ " &lt;&lt; price &lt;&lt; " FROM " &lt;&lt; dealer; </code></pre> <p>the only way I can think of doing this right now would be to have a bunch of inline functions that return strings and instead of having a char* table instead have a function table that looks up the inline functions. There must be an easier way tho.</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