dimanche 1 juin 2014

Inheriting classes from std::


Vote count:

0




There are many similar questions and I found both pro and against reasons to use this pattern so I am asking this here:


I need to make a JSON implementation in C++ (let's say that it is sort of like a homework). I was thinking to do it like this:



namespace JSON {
class JSON { };
class object : public JSON, public std::unordered_map<std::string,JSON> { };
class vector : public JSON, public std::vector<JSON> { };
class string : public JSON, public std::string { };
...
};


If you think about it, it all make sense. The JSON object "is-an" unordered_map, the JSON vector "is-a" vector and so on. Just that they are also a JSON value and, for example, a JSON vector can contain any type of JSON values (objects, vectors, strings etc.). You get a lot of benefits too, you can then just use the JSON "naturally" in C++ (you can have an actual std::string vector inside json["mystringlist"], json being actually an unordered_map).


I am not really a C++ expert but is there any specific reason why not to do this?



asked 16 secs ago






Aucun commentaire:

Enregistrer un commentaire