Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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?

like image 417
fxam Avatar asked Dec 06 '22 14:12

fxam


2 Answers

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 (&).

like image 99
Luchian Grigore Avatar answered Dec 21 '22 23:12

Luchian Grigore


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.

like image 32
Konrad Rudolph Avatar answered Dec 21 '22 23:12

Konrad Rudolph