Recently, I've learned about Pointers and so far, it seems like a glorified version of reference variable, is there any good uses for pointers that help make a code more efficient or something of that sort?
            Asked
            
        
        
            Active
            
        
            Viewed 59 times
        
    -1
            
            
        - 
                    A pointer is not a glorified reference. There are good uses for pointers. I recommend looking at code that use pointers. – Drew Dormann Feb 02 '22 at 21:02
- 
                    I believe the pointers were first, then came references :) Also, see https://www.geeksforgeeks.org/pointers-vs-references-cpp/ – Vlad Feinstein Feb 02 '22 at 21:13
- 
                    4It's the other way around. References are essentially pointers with some features removed. – HolyBlackCat Feb 02 '22 at 21:14
- 
                    A pointer is its own type. A reference is the type, and may be optimized away in a surprisingly high number of cases. A reference is more like an *alias* name for the same object. And now with rvalue references and move semantics, us C++ developers have even finer grained control and can match the performance of FORTRAN on FORTRAN's home turf (which previously had very much annoyed me... thank you Howard Hinnant!). – Eljay Feb 02 '22 at 22:07
1 Answers
3
            
            
        Both are "good", although in 2022, raw pointers in C++ are less useful than they once were. I go days without using them.
You are correct that T& and T* are similar. In particular, given T& fooRef and T* const fooPtr (where T* const is a pointer that cannot be changed) make it so that fooRef and *fooPtr are essentially the same. References can't be nullptr and cant be re-pointed. Pointers can be nullptr and can be re-pointed.
As @Macmade points out, pointers can point to the beginning of a contiguous buffer of many elements.
If you are new to C++, I'd recommend against using raw pointers. Almost everything can be done more safely with managed pointers, particularly std::unique_ptr<T>, which owns the thing it points to.
 
    
    
        Ben
        
- 9,184
- 1
- 43
- 56
- 
                    3And may point to a series of values instead of a single one, may have multiple dimensions, etc… Probably worth mentioning… – Macmade Feb 02 '22 at 21:06
