简体   繁体   English

指针可以在纯C ++中使用吗?

[英]Can pointers be used in pure C++?

I read pointer as ornament of C that makes C a special language. 我读pointer作为C装饰物,使C成为一种特殊的语言。 However, i also used pointers in C++. 但是,我也在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 ? 我想在C ++中使用指针比在C语言中有一些限制。由于指针是C的数据类型。如果要求我用纯C++编写程序,我可以使用指针吗?

Ofcourse you can use pointers in C++. 当然,您可以在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. 关于C ++中的指针的一件特殊事情是C ++提供了智能指针 ,可以使您的生活更轻松。 Prefer them over raw c like pointers. 优先于原始c之类的指针。

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. 实际上,对于更复杂的程序,很可能根本没有其他方法:如果您需要进行任何类型的动态分配(与new ),则将获得一个指针。

Also, C++ really is an extension of C. Most (if not all) valid C code is also valid C++ code. 同样,C ++确实是C的扩展。大多数(如果不是全部)有效的C代码也是有效的C ++代码。 That's for example one of the reasons C++ has a struct keyword: it needs to be backwards compatible with C. 例如,这就是C ++具有struct关键字的原因之一:它需要与C向后兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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