Represent json file with a set of different data members to a C++ class I want to parse a json file, where some elements are common each time while others are different based on a type. 
For example a message json:

{
    "title": string,
    "body": string,
    "type": int (0 for email, 1 for sms, 2 for terminal)
    "typeProperties": {
        // Different based on type value
    }
}


I ended up to the following code to represent the json object:

#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;

class Message {
    public:
        int id;
        std::string title;
        std::string body;
        int type; // 0 - Email, 1 - SMS, 2 - Terminal
};

class Email : public Message {
    public:
        std::string emailRecipients;
        std::string emailCC;
};
class Sms : public Message {
    public:
        std::string  phoneNumber; 
};
class Terminal : public Message {
    public:
        int terminalId;
};

std::vector&lt;Message*&gt; list_msg;

int main(int argc, char** argv) {
    Email mail;
    mail.id = 1;
    mail.title = "title1";
    mail.body = "body1";
    mail.type = 0;
    mail.emailRecipients = "text1";
    mail.emailCC = "text2";

    list_msg.push_back(&amp;mail);

    Terminal tms;
    tms.id = 2;
    tms.title = "title2";
    tms.body = "body2";
    tms.type = 1;
    tms.terminalId = 123;

    list_msg.push_back(&amp;tms);

    for (auto m : list_msg) {
        std::cout &lt;&lt; "id: " &lt;&lt; m-&gt;id &lt;&lt; ", type: " &lt;&lt; m-&gt;type &lt;&lt; std::endl;

        if (m-&gt;type == 0) {
            Email *tmp = static_cast&lt;Email*&gt;(m);
            std::cout &lt;&lt; "&gt;&gt;&gt;&gt; recipient: "  &lt;&lt; tmp-&gt;emailRecipients &lt;&lt; ", cc: " &lt;&lt; tmp-&gt;emailCC &lt;&lt; std::endl;
        } else if (m-&gt;type == 1) {
            Terminal *tmp = static_cast&lt;Terminal*&gt;(m);
            std::cout &lt;&lt; "&gt;&gt;&gt;&gt; terminalId: "  &lt;&lt; tmp-&gt;terminalId &lt;&lt; std::endl;
        }
    }
}


Questions:


Is the above code correct?
From performance perspective, can this be optimized?
If I don't use inheritance and have one class Message with a boost::variant member - what are the pros and cons of this approach?
Correctness

I'm not sure what exactly you mean by 'correct'. Personally, I regard this code as poorly written, so I would reject it during peer review at my work. It's using C++ as if it were C, and violating several OOA/D heuristics.

Class Message knows it has subclasses

Class Message shouldn't have that 'type' member - a base class should not know anything about its subclasses, not even that any subclasses exist. At the moment, class Message knows that it has &lt;= 2^(sizeof(int)*8 - 1) subclasses (else that int would need to be a 'uint128_t' or something). And if you changed that 'int' to be an 'enum message_type', which is what it really is, then Message would know of the existence of each of its subclasses, which would be even worse: addition of a subclass would require modifying the base class.

It seems to me you can simply delete that 'type' member, and the lines that set its value, without any loss of functionality.

The take-away point is: C++ objects (instances of some class) already 'know' the class of which they are an instance. You don't need to store this information a second time using class data members.

Encapsulation (Google for this term)

Classes shouldn't have public data - where you're setting public members directly, you should either be passing arguments to constructors, or (less preferably) calling setters after instance construction.

Performance

Do you really care about performance of this code? Think very hard about why this code is performance-critical. Chances are, it's not.

If this code really is performance-critical, for example it's executed thousands of times per second on a small embedded processor in a real-time system on which lives depend, then consider instead using an optimised binary format instead of JSON, using superior algorithms elsewhere in your code, offloading work to other parts of the system that are less time-critical or have more resources, or simply reducing the scope of the project to something easier to accomplish.

Pros and cons of using inheritance (instead of a variant member)

Pros of inheritance: Uses C++ in the way its designers intended and in the way later readers will expect. Allows for polymorphism (Google for this term). Promotes future code re-use and eases modification, by making it easier to add additional subclasses later on, or modify the superclass behaviour/implementation.

Cons of inheritance: I can't think of any. You'd be mimicking inheritance by use of containment, which may be sadly necessary in some circumstances but seems worthless to you in this circumstance.

Strange JSON

The JSON is redundant, which means it's possible for it to be inconsistent. What does it mean for the JSON to contain a 'type' attribute with the value 0, meaning email, but then inside its 'typeProperties' sub-object, it contains all of the the attributes appropriate to an SMS and none of the attributes appropriate to an email?

If this isn't possible, then why does the JSON design permit it? Why not just treat it as an SMS if it has SMS members, or an email if it has email members, and dispense with the 'type' attribute?

If this is possible, then how are you intending to handle it?