Is there a way in C ++ to specify any type * as the type of the template, for example, using a wildcard or some kind of keyword.
I remember in Java we can do this using? wildcard as a generic type, such as HashMap<Long, ?>
.
I am trying to better explain the question using code example. Suppose we have the following notation class:
template<typename T> class Record { private: T content; long size; public: Record(T _content, long _size) { this->content = _content; this->size = _size; } T getContent() { return this->content; } long getSize() { return this->size; } };
and suppose we want to use instances of the above class without specifying the exact type (I used "in the next class"), simply because it is useless if the Computer class uses only the Record::getSize()
method:
class Computer { public: long long computeTotalSize(vector<Record<?>> recordVector) { long long totalSize = 0; for (vector<Record<?>>::iterator it = recordVector.begin() ; it != recordVector.end(); ++it) totalSize += (*it).getSize(); return totalSize; } };
source share