adding shared ptr implementation

This commit is contained in:
Jidong Xiao
2025-04-18 04:07:02 -04:00
committed by JamesFlare1212
parent 64aa4ba11c
commit 415b60e129

View File

@@ -0,0 +1,39 @@
#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;
}