Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In C++, pointers generally have a lot more functionality associated with them than is really necessary. A class template that wraps a pointer and removes most of the less-used features (arithmetic, indexing, deletion). Each function could then decide how powerful a pointer it requires and take the appropriate type.

Would using such types make sense in an actual project, though? Are errors caused by unintentional arithmetic/deletion common enough to justify the additional effort?

To clarify: I am considering this as a system alongside existing weak_ptr, shared_ptr, unique_ptr -- those three are great when ownership is involved, but sometimes non-owning nullable or reseatable references are useful.

share|improve this question
2  
Why would you want to do this? If you're going to move away from raw pointers, why not consider existing smart pointers (e.g. boost::shared_ptr). –  Yusubov Sep 13 '12 at 22:11
1  
@ElYusubov better yet std::shared_ptr why not go standard –  jozefg Sep 14 '12 at 3:14
1  
What problems are you actually trying to solve. This seems more like a solution looking for a problem. Arithmetic is covered by container usage. Deletion is covered by smart pointers usage. –  Loki Astari Sep 14 '12 at 3:15
    
@LokiAstari: Exactly: all of the more advanced problems pointers solve are already solved with dedicated classes. This leaves the cases where the functionality is a subset (basically: indirection + assignability). I am wondering if using a type that restricts that case to only support those two features is worth the effort. –  Anton Golov Sep 14 '12 at 9:06
    
I am saying don't try to implement solutions to problems that don't exist. If you have some specific scenarios then it becomes easier to discuss the usage or frame then in already existing wrappers. –  Loki Astari Sep 14 '12 at 16:14

3 Answers 3

up vote 2 down vote accepted

For me "non-owning pointers" are nearly non-existant. If I will be storing a pointer to an object in a member variable, then there is some kind of ownership in play, even if it is weak ownership. If this is for passing arguments to a method, then there are references (which have none of the problems you mention).

The only exception I can think of is when iterating through a raw array, maybe serializing/deserializing binary data. In that case I want to be able to do pointer math, but not deletion. But it would be incredibly difficult to accidently delete a pointer, especially when I hardly use delete anyways!

share|improve this answer

Not sure about arithmetic, but pointer ownership(who deletes it?) is a major source of bugs.

You might be interested in learning about smart pointers. They definitely change some of the pointer semantics, which makes them very useful.

As for nullable types, they might be useful. See Boost.Optional.

share|improve this answer
1  
I am aware of smart pointers and use them extensively -- they solve the issue of ownership fairly well. My question is about whether dividing non-owning pointers further would be of use. –  Anton Golov Sep 13 '12 at 22:31

Wrapping a pointer is close to the right idea. Explore value classes. Build your classes so that the pointer is an implementation detail of the actual behavior. std::string and std::vector are good examples of value classes. You can copy them, assign them. They work like you expect an object to work.

This talk given by Sean Parent explores how to make polymorphism an implementation detail. I've used this pattern several times. I find it quite wonderful.

Value semantics are how to avoid pointer mistakes. No pointers, no mistakes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.