Morgan Stanley Interview Question Financial Software Developers
0of 0 votesimplement shared_ptr in C++
template <class T> class shared_ptr { private: T* _t; int *count; public: shared_ptr(T* t){ _t = t; count = new int(1); } template <class D> shared_ptr(shared_ptr<D>& d){ _t = d.Get(); count = d.GetCount(); *count = (*count)+1; } ~shared_ptr() { *count = (*count)-1; if(*count == 0) delete _t; } template <class D> shared_ptr<T>& operator= (shared_ptr<D>& d) { if( this->Get() != d.Get() ) { *count = (*count)-1; if( *count == 0) delete _t; _t = d.Get(); count = d.GetCount(); *count = *count + 1; } return *this; } T* Get() { return _t; } int* GetCount() { return count; } };
Country: United States
Interview Type: In-Person
#include<iostream>
template<class T>
class sharedPtr
{
T* ShrdPtr;
mutable int* refCount;
public:
sharedPtr( T* ptr ) { ShrdPtr = ptr; refCount = new int(); *refCount = 1; }
~sharedPtr( )
{
std::cout << "inside destructor refCount = " << *refCount << "\n";
if( --(*refCount) == 0 )
delete ShrdPtr;
}
T& operator=( const sharedPtr& ptr );
sharedPtr( const sharedPtr& ptr );
T* operator->( ) { return ShrdPtr; }
T& operator*() { return *ShrdPtr; }
};
template<class T>
T& sharedPtr<T>::operator=( const sharedPtr& ptr )
{
std::cout << "inside assignment operator\n";
this->ShrdPtr = ptr.ShrdPtr;
(*(ptr.refCount))++;
this->refCount = ptr.refCount;
return *this;
}
template<class T>
sharedPtr<T>::sharedPtr( const sharedPtr& ptr )
{
std::cout << "inside copy constructor\n";
this->ShrdPtr = ptr.ShrdPtr;
(*(ptr.refCount))++;
this->refCount = ptr.refCount;
}
int main( int argc, char* argv[] )
{
sharedPtr<int> p = new int;
{
sharedPtr<int> q = p;
}
return 0;
}
Here is my Implementation of shared ptr:
// shared_pointer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <exception>
using namespace std;
template <class T> class sh_ptr
{
private:
T* ptr;
static int count;
public:
sh_ptr() {
ptr = new T;
count++;
cout << "count = " << count << endl;
}
~sh_ptr() {
// check count before deleting the pointer
cout << " count before = " << count << endl;
count--;
cout << " count after = " << count << endl;
if ( !count)
{
if (ptr) delete ptr;
ptr = NULL;
}
}
// implement sssignment and copy operation because shared pointer
// does copy and assignment. So it is named as shared pointer
// make sure that the copy constructor will invoke additional
// pointer to that heap memory and one additional counter
// no deep copy is required. For assignment operator
// we may require a deep copy as they already point to two different memory location.
// or we may not write a assignment operator
// We have to implement operator-> to exhibit the pointer behavior.
sh_ptr(sh_ptr& d){
ptr = d.ptr;
count++ ;
}
sh_ptr& operator=(sh_ptr& d){
// here is a memory leak for new, so first delet the new
// before assigning the pointer to another memory location
delete d.ptr;
ptr = d.ptr;
count++ ;
return *this;
}
void fun()
{
cout << " this is a fun" << endl;
}
sh_ptr<T>* operator->()
{
return (this);
}
};
template < class T> int sh_ptr<T>::count=0;
int main(int argc, char* argv[])
{
{
sh_ptr<int> obj;
obj->fun();
sh_ptr<int> obj2 = obj;
sh_ptr<int> obj7;
obj7 = obj;
}
cout << " new object" << endl;
sh_ptr<float> obj3;
sh_ptr<double> obj4;
return 0;
}

many errors in your code.
- jianbozhu on May 12, 2012 Edit | Flag Replyfor instance. ctor should never return and its parameter must be const reference.