C ++:什么是使用指针是“好主意”(TM)的场景? [英] C++: What are scenarios where using pointers is a "Good Idea"(TM)?

查看:226
本文介绍了C ++:什么是使用指针是“好主意”(TM)的场景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

指针的常用用途?

我仍然在学习基础知识C ++,但我已经知道了足够的有用的小程序。

I am still learning the basics of C++ but I already know enough to do useful little programs.

我理解指针的概念,我在教程中看到的例子对我有意义。然而,在实际层面上,作为一个(前)PHP开发人员,我还没有信心在我的程序中实际使用它们。

I understand the concept of pointers and the examples I see in tutorials make sense to me. However, on the practical level, and being a (former) PHP developer, I am not yet confident to actually use them in my programs.

事实上,有没有感觉到需要使用任何指针。我有我的类和函数,我似乎做的完全没有使用任何指针(更不用说指针指针)。

In fact, so far I have not felt the need to use any pointer. I have my classes and functions and I seem to be doing perfectly fine without using any pointer (let alone pointers to pointers). And I can't help feeling a bit proud of my little programs.

仍然,我注意到我在C ++上最缺少的一个C ++的最重要的功能,一个双边的:指针和内存管理可以创建破坏,看似随机崩溃,很难找到错误和安全漏洞...但同时,正确使用,他们必须允许聪明和高效

Still, I am aware that I am missing on one of C++'s most important feature, a double edged one: pointers and memory management can create havoc, seemingly random crashes, hard to find bugs and security holes... but at the same time, properly used, they must allow for clever and efficient programming.

因此:请告诉我我不是使用指针所缺少的。

So: do tell me what I am missing by not using pointers.

什么是好的场景其中使用指针是必须的?

它们允许您做什么,否则您不能这样做?

在哪种方式他们让你的程序更有效率?

What are good scenarios where using pointers is a must?
What do they allow you to do that you couldn't do otherwise?
In which way to they make your programs more efficient?

而指针指针呢?

推荐答案

指针通常用于C ++。对他们很舒服,将帮助你理解更广泛的代码。如果你可以避免它们是伟大的,但是,随着你的程序变得越来越复杂,你可能需要它们,即使只是与其他库的接口。

Pointers are commonly used in C++. Becoming comfortable with them, will help you understand a broader range of code. That said if you can avoid them that is great, however, in time as your programs become more complex, you will likely need them even if only to interface with other libraries.


  • 主要用于指向动态分配的内存(由 new 返回)。

它们允许函数接受无法复制到堆栈的参数,因为它们太大或无法复制,例如系统调用返回的对象。

They allow functions to take arguments that cannot be copied onto the stack either because they are too big or cannot be copied, such as an object returned by a system call. (I think also stack alignment, can be an issue, but too hazy to be confident.)

在嵌入式编程中,它们用于指代硬件(例如硬件)

In embedded programing they are used to refer to things like hardware registers, which require that the code write to a very specific address in memory.

指针也用于通过它们的访问对象来访问对象。基类接口。也就是说,如果我有一个从A类 B类派生的类B:public A {} 。这是一个对象B的实例可以通过提供其地址到A类的指针来访问,就像它在A类中一样。 A * a =& b_obj;

Pointers are also used to access objects through their base class interfaces. That is if I have a class B that is derived from class A class B : public A {}. That is an instance of the object B could be accessed as if it where class A by providing its address to a pointer to class A, ie: A *a = &b_obj;

使用指针作为数组上的迭代器是一个C语言。这在旧的C ++代码中可能仍然是常见的,但是可能被认为是STL迭代器对象的不良表达。

It is a C idiom to use pointers as iterators on arrays. This may still be common in older C++ code, but is probably considered a poor cousin to the STL iterator objects.

如果需要与C代码交互,你将不变地需要处理指针,用于指向动态分配的对象,因为没有引用。

If you need to interface with C code, you will invariable need to handle pointers which are used to refer to dynamically allocated objects, as there are no references. C strings are just pointers to an array of characters terminated by the nul '\0' character.

一旦你的字符串感觉舒适的指针,指针的指针将不会这么糟糕。最明显的例子是 main()的参数列表。这通常被声明为 char * argv [] ,但我已经看到它(法律上我相信) char ** argv

Once you feel comfortable with pointers, pointers to pointers won't seem so awful. The most obvious example is the argument list to main(). This is typically declared as char *argv[], but I have seen it declared (legally I believe) as char **argv.

声明是C风格,但它说我有一个指向char的指针数组。它被解释为C风格字符串(由nul'\0'字符终止的字符数组)的任意大小的数组(大小由argc携带)。

The declaration is C style, but it says that I have array of pointers to pointers to char. Which is interpreted as a arbitrary sized array (the size is carried by argc) of C style strings (character arrays terminated by the nul '\0' character).

这篇关于C ++:什么是使用指针是“好主意”(TM)的场景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆