C++ doesn't have such a "universal base-class" like Java's Object class. So, this mechanism has to be created some other way.
For a truly "anything" container, the only general solution is to use something like boost::any from the Boost.Any library (which might also be supported by some compilers as std::experimental::any, as described here). There is obviously overhead to using this, but I assume this is something you are prepared to live with.
Here is how you could use it:
#include <vector>
#include <string>
#include <iostream>
#include <typeinfo>
#include <boost/any.hpp>
int main() {
std::vector< boost::any > contents;
int n = 1991;
std::string str = "mfso";
contents.emplace_back(n);
contents.emplace_back(str);
for(auto& x : contents) {
if( x.type() == typeid(int) )
std::cout << " Int: "
<< boost::any_cast< int >(x) << std::endl;
else if( x.type() == typeid(std::string) )
std::cout << " String: "
<< boost::any_cast< std::string& >(x) << std::endl;
};
};
Ideally, you should avoid using something like boost::any if you can solve your problem some other way. For example, if all types of objects are related (or can be related) through some base-class, then you should use that. Or, if you have a limited number of types that you expect to store in the container, then you can use something like boost::variant instead (see docs).
if()type branches that OO is supposed to avoid. To be OO you really need the types stored in the container to be strongly related so each type is a variety of some common base type interface.