I come from a Python background, but I was reading up on the kind of objects and data structures available in the C++ standard library and I see that the declaration for say, a vector of strings:
vector<string> names(10);
Would indicate a vector object initialized to hold 10 objects of type string. My questions are:
- Does the
<string>part of the declaration have an abstraction to C++ class system, i.e. is it a in built syntax for the vector object or part of some feature of the class system that can be used on user-defined class definitions? - Am I right in assuming that the brackets followed by the name are the arguments passed to the constructor function?
- Is
stringa built-in type likeintor an object defined by the standard library. And thus, is there a difference between built-in types and other objects (for instance all “types” are objects in Python)?
There are two sorts of parameters to
std::vectorobjects:std::vectorthe value type and the allocator types with the latter being defaulted. That is,std::vector<std::string>actually happens to bestd::vector<std::string, std::allocator<std::string> >.std::vector<...>takes quite a few combinations of those. In the above quoted use the10is the number of initial elements given to the vector.So, to answer your concrete questions:
stringhappens to bestd::stringand is nothing special at all. You can use any user-defined type which models certain concepts (e.g. the type needs to beCopyConstructible).[and]but the term is ambiguous).string, well, actuallystd::stringis not defined as a built-in type but it is a type from the standard C++ library. How the standard C++ library types are implemented is pretty much up to the C++ implementation, however, i.e., an implementation could choose to make it built-in (as long as it can still be used like a class type).In C++ there are a few differences between built-in types and class types but with C++ 2011 it is gets pretty close to being able to create class types which behave like built-in types. The primary difference is that it is possible to take the address of certain members of class types while the same “members” are not accessible for built-in types. Another difference is that built-in types don’t need to be declared (actually, they cannot be declared) while class types need to be declared and/or defined (depending on how these are used).