• Advertisement

Archived

This topic is now archived and is closed to further replies.

Smart Pointer Constructor help required

This topic is 5495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there. I am currently writing a smart pointer class to fulfill my specific needs. These are 1) explicit ownership. There are two types of pointer, strong and weak. strong can only own scoped data, and weak can only manipulate that data 2) release version is identical to using pointers. this rules out reference counting and extra validation data within the classes. 3) debug version will allow memory checks (havent yet decided if it should do bounds checking) on every pointer assignment or usage. I have read up on many sites about different offerered smart pointers to try to find something that fulfills my requirements but so far have failed. This leads to two questions: 1) have I made a mistake and such code already does exist? or 2) I have a problem in my implementation that I would bl glad to get some help with. The problem is this... an example of usage showing my problem:
  
void Func( Ptr::Wk< int> wk)
{
	*wk = 1000;
}

void ConstFunc( Ptr::Wk< const int> wk)
{
	int blah = *wk;
}

void main( void)
{
	Ptr::Str< int> owner1 = new int[ 10];
	Ptr::Wk< int> modifier1 = owner1;
	*++modifier = 32; // etc, just example of usage

	Func( modifier1); // etc, just example of usage


	Ptr::Str< const int> owner2 = new int[ 10];
	// Func( owner2); // this is currently a compile time error which is what i want

	ConstFunc( owner2); // this is fine - automatic conversion from Str< const int> to Wk< const int>


	ConstFunc( modifier1); // this is the problem...

	// conversion from Wk< int> (or Str< int>) to Wk< const int> required

	// BUT the solution MUST still disallow Wk< const int> to Wk< int>

}
   
I have tried various solutions to allow the conversion from to without success, as they all end up with allowing some other unwanted conversion. One thing I was hopeful for was using a template constructor that would always do a const_cast and then use template specialisation to disallow the const to non-const cast, but I couldnt get anything watertight. Thanks for your time. [edited by - Shrew on February 3, 2003 9:11:28 AM]

Share this post


Link to post
Share on other sites
Advertisement
I think you hit one of the fundamental problems in templates. The constantness in the template parameter makes a totally different type.

I don''t think it''s solvable. Alas, I also tried making an ultimate smart pointer wrapper but also got in many problems with the conversions. Have you read Scott Meyer''s Effective C++ (it may be More Effective C++)? It has an chapter on how hard it is to implement a generic smart wrapper.

Share this post


Link to post
Share on other sites
Why don't you just make a constructor that allows all implicit typecasting of the held pointer-type?


  
template<class T>
class Wk
{
public:
template<class U>
Wk(const Wk<U>& other): m_ptr(other.m_ptr) {}
};


[edited by - Dew_LED on February 3, 2003 2:11:38 PM]

Share this post


Link to post
Share on other sites
fantastic - i havent checked all circumstances but that appears to be the case. I had done something similar already, but very stupidly took it too far - I was doing this:

template< typename CCT> Wk( const Str< CCT>& t) : _t( const_cast< T>( t._t)) {assert( NULL != _t);}
template< typename CCT> Wk( const Wk< CCT>& t) : _t( const_cast< T>( t._t)) {assert( NULL != _t);}

but this would allow a lose of constness. Im slapping myself silly now - I was getting a bit annoyed at myself.

There are many conditions to check but I''ll give them a go - the first few appear to be work

Shrew

Share this post


Link to post
Share on other sites
quote:
Original post by petewood
take a look at boost''s smart pointers


I already have - none of boosts pointers fulfill my requirements which were listed above, thanks

Share this post


Link to post
Share on other sites

  • Advertisement