6
I was studying smart pointers and based on this blog: https://murilo.wordpress.com/2008/12/15/smart-pointers-os-ponteiros-espertos-do-c/
#include <iostream>
template <class T> class SmartPointer
{
T *ptr; public:
SmartPointer(T *p):ptr(p) {}
~SmartPointer() {delete ptr;}
T& operator*() {return *ptr;}
T* operator->() {return ptr;}
};
int main()
{
SmartPointer str(new std::string("Testando Smart Pointers"));
std::cout << *str << " size: " << str->size() << std::endl;
}
but when compiling the same it returns this error:
ex2.cxx: In function ‘int main()’: ex2.cxx:20:18: error: missing
template arguments before ‘str’
SmartPointer str(new std::string("Testando Smart Pointers"));
^~~ ex2.cxx:21:19: error: ‘str’ was not declared in this scope
std::cout << *str << " size: " << str->size() << std::endl;
then the example itself is bad yes but what I thought was to try to understand the functioning of this to do in my classes and structs in c++ not to have to keep manipulating pointer or worrying about deleting pointers I found interesting the proposal but I still have no idea of how to do as correctly as possible for future use in the classes or programs developed in the language...
– dark777
More or less. Worry should always exist. This helps, but it is not something bulletproof. There’s a lot of things that could go wrong even with these pointers. Of course knowing how to use, analyzing whether it can not be some unwanted effect will cool, but it is far from simple as it is marketado
– Maniero