简体   繁体   中英

Can pointers be used in pure C++?

I read pointer as ornament of C that makes C a special language. However, i also used pointers in C++. I guess there are some limitations while using pointer in C++ than in C. As pointers are data types of C. If i am asked to write a program in pure C++ , can i use pointers ?

Ofcourse you can use pointers in C++. There are some instances where pointers are the only way out. For eg: to use dynamic memory allocations.

One special thing about pointers in C++ is C++ provides Smart pointers which can make your life easier. Prefer them over raw c like pointers.

Bottomline is:
Use what suits your implementation needs. Don't adhere to fixed rules there are none really.

Yep. In fact, for more complex programs, it's quite possible that there simply isn't any other way: if you need to do any kind of dynamic allocation (the kind you do with new ), you'll get a pointer.

Also, C++ really is an extension of C. Most (if not all) valid C code is also valid C++ code. That's for example one of the reasons C++ has a struct keyword: it needs to be backwards compatible with C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM