Files
CSCI-1200/lectures/27_garbage_collection/shared_ptr2.cpp
2025-04-29 14:42:52 -04:00

40 lines
927 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
#include <string>
template <class T>
class shared_ptr {
public:
shared_ptr(T* ptr){
ptr_ = ptr;
}
~shared_ptr(){
if(ptr_ != nullptr){
delete ptr_;
}
}
T& operator*(){
return *ptr_;
}
// operator->() is a unary operator — it only uses the left-hand operand (s1) to get access to the underlying object.
T* operator->(){
return ptr_;
}
private:
T* ptr_;
};
int main(){
shared_ptr<int> age(new int(20));
std::cout << "age is " << *age << std::endl;
shared_ptr<std::string> s1(new std::string("test"));
// compiler will interpret s1->length() as (s1.operator->())->length(), and thats just how C++ handles overloaded operator->()
// and s1.operator->() returns a std::string*
// therefore we now have: (std::string*)->length(), which is just normal pointer behavior — calling length() on the string.
std::cout << "length is " << s1->length() << std::endl;
return 0;
}