fxam
fxam

Reputation: 3982

Is it acceptable to always use pointers instead of references, to be easily converted to smart pointers if needed?

I know references are preferred over pointers, but it is very tedious to change all those "." to "->" when needed. So is it an acceptable practice to always use pointers instead of references, to be easily convert them to smart pointers if needed? Or is there any way to quickly change a reference to pointer?

Upvotes: 4

Views: 237

Answers (4)

James Kanze
James Kanze

Reputation: 153909

Using pointers instead of references will not make later conversion to smart pointers any easier. You can't just replace all pointers with smart pointers and expect anything to work; the semantics of smart pointers is significantly different from that of pointers (and usually not what you want).

Using pointers instead of references in classes with value semantics does make sense, since it's difficult, if not impossible, to implement the expected assignment semantics with references. Smart pointers have nothing to do with it.

Upvotes: 0

Konrad Rudolph
Konrad Rudolph

Reputation: 545528

Change the architecture just to facilitate text replacements in source code?

No, this is never a valid design decision. And as Luchian has explained, this doesn’t seem like a really existing consideration to begin with.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258568

So is it an acceptable practice to always use pointers instead of references, to be easily convert them to smart pointers if needed

No. In general, always rules are always bad. (including this one). Why would you want to convert a reference to a smart pointer? If it's a reference, you don't need to worry about memory management, and that's the purpose of a smart pointer.

Or is there any way to quickly change a reference to pointer?

Yes, taking it's address (&).

Upvotes: 15

Jonathan Wood
Jonathan Wood

Reputation: 67195

The point of using references is to make things slightly easier for you because you can use them like a regular, non-pointer variables.

But if it's not easier, or even harder, I see absolutely no reason to use them.

There's nothing wrong with using pointers.

Upvotes: -1

Related Questions