//
// main.cpp
//
// Created by Rogiel Sulzbach on 7/22/14.
// Copyright (c) 2014 Rogiel Sulzbach. All rights reserved.
//
// http://w...content-available-to-author-only...l.com/
//
#include <iostream>
#include <vector>
/**
* Base class for the template
*/
class MyBaseClass {
public:
/**
* Prints the value to the console
*/
virtual void print() = 0;
};
/**
* A template that holds a value
* @tparam T the value type
*/
template<typename T>
class MyTemplatedClass : public MyBaseClass {
private:
/**
* The value storage
*/
T value;
public:
/**
* Creates a new instance
*
* @param value the value
*/
MyTemplatedClass(T value) : value(value) {
};
// implementation
virtual void print() {
std::cout << value << std::endl;
}
};
int main(int argc, const char * argv[]) {
// creates two objects
MyTemplatedClass<std::string> myObj1("Hello World!");
MyTemplatedClass<int> myObj2(1000);
// inserts the two objects into a vector
std::vector<MyBaseClass*> myVector;
myVector.insert(myVector.end(), &myObj1);
myVector.insert(myVector.end(), &myObj2);
// iterate them and call print()
for(auto obj : myVector) {
obj->print();
}
// profit.
return 0;
}