Sign in to follow this  

Smart Pointer Constructor Strangeness

Recommended Posts

Hi, I am writing my own smart pointer class, which seems to be working so far, but I was a little confused as there was a point I was expecting the compiler to complain when it didn't. I have the test code...
	MyTest* test2=new MyTest();
	MyTestPtr testp1=test2;
	MyTestPtr testp2=test2;
	MyTestPtr testp3=testp2;

which works fine when I have the constructors...
	SmartPointer(T* in_object);
	SmartPointer(SmartPointer& in_smartPointer);

but when I comment out the second constructor it STILL works. Why wouldn't the last line on the first section of code (MyTestPtr testp3=testp2) fail if there wasn't a constructor for the SmartPointer? BTW, MyTestPtr is just a typedef for a smart pointer to the test class MyTest (ie typedef SmartPointer<MyTest> MyTestPtr). Thanks

Share this post

Link to post
Share on other sites
It's probably writing a default copy constructor for you, which, with smartpointers, or anything that holds a pointer, is bad. Don't comment out that constructor.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this