Here is a Template class I've made to catch up on templates
template <typename T>
class Myclass {
std::vector<T> v;
public:
Myclass(): v({0}){;}
Myclass(const Myclass& M)
{
this->v={1,2}; //ignore this I'm gonna use int for testing
}
~Myclass()
{
std::cout<<"Destructor";
}
void setter(T i)
{
v.push_back(i);
}
auto getter() -> decltype(T)
{
return v.back();
}
};
I would like to have the getter function return the last element in v
and pop_back()
. Now I could store the back()
value in a variable and just pop_back()
. But I was hoping I could come up with smart pointers to do the same. Here's what I came up with
auto getter() -> decltype(v.back())
{
std::unique_ptr<T> p1 (new T);
*p1=v.back();
v.pop_back();
return *p1;
}
and my main is
Myclass<int> m1;
m1.setter(5);
Myclass<int> m2=m1;
std::cout<<m1.getter()<<m1.getter()<<std::endl;
std::cout<<m2.getter()<<m2.getter()<<m2.getter();
Yeah I want to do it with local smart pointers so that I can "be sure" of the objects destruction. Now My program compiles but runs endless.