为什么指针? [英] Why pointers?

查看:59
本文介绍了为什么指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我知道如何使用指针之前,我想知道 - 首先,

如何使用它们。第二,它们的价值是多少。


既然我至少对如何使用它们有了很好的了解,那么我还是想知道b $ b仍然在想他们真正有价值的是什么?


我会承认 - 能够间接分配价值非常性感,

但是我是什么试图说明为什么它比直接分配

值更好。


声明一个指针,指向一个数组的某个元素,取消引用

它。哇 - 当你可以的时候看似很多工作:myArray [5]

= 6.


任何评论都将不胜感激。你可以说,我是C ++的新手 -

但我喜欢它!

Before I had a good idea how to use pointers, I was wondering - first,
how to use them. And second, of what value would they be.

Now that I have at least a pretty good idea of how to use them, I am
still wondering of what real value are they?

I''ll admit - its pretty sexy to be able to assign values indirectly,
but what I''m trying to figue out is why it is any better than assigning
the values directly.

Declare a pointer, point it at some element of an array, dereference
it. Whew - seems like a lot of work when you can just: myArray[5]
=6.

Any comments would be appreciated. As you can tell, I am new to C++ -
but I love it!

推荐答案

* arganx:
在我知道如何使用指针之前,我想知道 - 首先,
如何使用它们。第二,它们具有什么价值。

现在我至少对如何使用它们非常了解,我仍然想知道它们的实际价值是什么? br />
我会承认 - 能够间接地分配价值非常性感,
但是我想弄清楚的是为什么它比分配更好
直接的值。

声明一个指针,指向一个数组的某个元素,取消引用它。哇 - 当你可以的时候看似很多工作:myArray [5]
= 6。
Before I had a good idea how to use pointers, I was wondering - first,
how to use them. And second, of what value would they be.

Now that I have at least a pretty good idea of how to use them, I am
still wondering of what real value are they?

I''ll admit - its pretty sexy to be able to assign values indirectly,
but what I''m trying to figue out is why it is any better than assigning
the values directly.

Declare a pointer, point it at some element of an array, dereference
it. Whew - seems like a lot of work when you can just: myArray[5]
=6.




< url:

http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01__alpha3.doc.pdf>


你唯一能提到的数组是他们没有讨论过... ;-)


-

答:因为它搞砸了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问: usenet和电子邮件中最烦人的事情是什么?



<url:
http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01__alpha3.doc.pdf>

where the only mention you will find of arrays is that they''re not
discussed... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


arganx写道:
之前我有一个好主意如何使用指针,我想知道 - 首先,
如何使用它们。第二,它们具有什么价值。

现在我至少对如何使用它们非常了解,我仍然想知道它们的实际价值是什么? br />
我会承认 - 能够间接地分配价值非常性感,
但是我想弄清楚的是为什么它比分配更好
直接的值。

声明一个指针,指向一个数组的某个元素,取消引用它。哇 - 当你可以的时候看似很多工作:myArray [5]
= 6.

任何评论都将不胜感激。正如你所知,我是C ++的新手 -
但我喜欢它!
Before I had a good idea how to use pointers, I was wondering - first,
how to use them. And second, of what value would they be.

Now that I have at least a pretty good idea of how to use them, I am
still wondering of what real value are they?

I''ll admit - its pretty sexy to be able to assign values indirectly,
but what I''m trying to figue out is why it is any better than assigning
the values directly.

Declare a pointer, point it at some element of an array, dereference
it. Whew - seems like a lot of work when you can just: myArray[5]
=6.

Any comments would be appreciated. As you can tell, I am new to C++ -
but I love it!




一些随机的想法,可能是不完整的,没有任何特定的顺序:


1)指针让你从函数中得到一些东西。例如。


std :: vector< int> v;


void foo(std :: vector< int> * x)...


如果要打造很多东西在foo()中你可能不希望

按值返回它。

2)是的,在#1中做的C ++ / OO越多就是使用引用。

但是,有一个优势指针超过了引用,就是能够将b $ b重置它们。如果这可能是你的一个因素...

3)指针可以更快地进行数组索引。考虑一个类型为foo的$ / b $ b对象的数组A.要通过A [i]访问元素,编译器

实际上计算A(a foo *)+ sizeof(foo)* i;这是很多工作。

假设你有一个迭代器...我的意思是指针;-) ...到foo,

你会在那里。因此,如果需要,使用指针通过数组索引可以节省一些时间。 (显然是第一个配置文件)

4)指针让你做酷OO的东西;-)如通过指向基类类型的指针访问

的某个派生类型的对象。从而获得虚拟函数调度,多态性,代码重用,

等的好处。是的,引用可以大致相同的方式使用。

5)如果对象太大而无法静态创建

或作为局部变量在堆栈上,则可能需要指针。在这里,参考资料可能会有所帮助。


6)指针让你明确而有意地控制

的生命周期超出{}可以为你做的对象本地范围。

7)指针让你在你的

代码中引入各种有趣的错误。 ;-)



Some random thoughts, probably incomplete and not in any particular order:

1) Pointers let you get something back out of a function. Eg.

std::vector<int> v;

void foo(std::vector<int>* x) ...

If you''re going to build up a lot of "stuff" in foo() you may not want
to return it by value.
2) Yes, the more C++ / OO thing to do in #1 is to use a reference.
However, one advantage pointers have over references is the ability to
reseat them. If and when that may be a factor for you...
3) Pointers can be faster for array indexing. Consider an array A of
objects of type foo. To access an element via A[i] the compiler
actually calculates A (a foo*) + sizeof(foo) * i; That''s a lot of work.
Suppose you just had an iterator ... I mean pointer ;-) ... to a foo,
you''d be there. So using pointers to index through arrays can save you
a little time, if you need it. (obviously profile first)
4) Pointers let you do cool OO stuff ;-) such as access an object of
some derived type via a pointer to a base class type. Thereby reaping
the benifits of virtual function dispatch, polymorphism, code reuse,
etc. Yes, references can be used in much the same way.
5) Pointers may be necessary if an object is too big to create staticly
or on the stack as a local variable. Here again, references may help.

6) Pointers let you explicitly and intentionally control the lifetime of
an object beyond what {} can do for you in a local scope.
7) Pointers let you introduce all kinds of interesting bugs into your
code. ;-)


arganx写道:
在我有一个好主意如何使用指针之前,我想知道 - 首先,
如何使用它们。第二,它们具有什么价值。

现在我至少对如何使用它们非常了解,我仍然想知道它们的实际价值是什么?


它们有很多用途,但通常只在必要时使用它们。他们在C ++中没有像在C中那样使用
,因为你在C ++中有引用

也间接地解决,并且经常使用std :: string代替char *。

他们仍然拥有的一些用途包括:

- 每个''new''表达式返回一个指针,并且有很多这些用于

C ++程序

- 多态(虚函数类型)需要指针或

引用,但引用通常不合适,例如,你可以有一个

的Shape *集合,但不是Shape&

- 迭代一个数组(仍然有它们的用途)

- char *或const char *其中std :: string是不必要的

- 给定操作系统中的API调用有时需要或返回一个地址

- 需要重新安装,因为引用可以''重新安排

- 需要空值,因为参考文献没有一个

我会承认 - 它非常性感才能够间接地分配值,
但我想弄清楚的是为什么它比直接分配值更好。


如果你可以直接完成任务,你就不会使用指针。

声明一个指针,指向一个数组的某个元素,取消引用
它。哇 - 当你可以这么做时似乎很多工作:myArray [5]
= 6.
Before I had a good idea how to use pointers, I was wondering - first,
how to use them. And second, of what value would they be.

Now that I have at least a pretty good idea of how to use them, I am
still wondering of what real value are they?
They have many uses, but usually you only use them when you have to. They
aren''t used as much in C++ as in C because you have references in C++ that
also address indirectly, and std::string is often used instead of char *.
Some of the uses they still have include:
- every ''new'' expression returns a pointer, and there are plenty of those in
C++ programs
- polymorphism (of the virtual function kind) requires a pointer or a
reference, but references are often unsuitable, e.g., you can have a
collection of Shape*, but not Shape&
- iterating over an array (which still have their uses)
- char * or const char * where std::string is unnecessary
- API calls in a given OS sometimes require or return an address
- where reseating is necessary, since references can''t be reseated
- where a null value is needed, since references don''t have one
I''ll admit - its pretty sexy to be able to assign values indirectly,
but what I''m trying to figue out is why it is any better than
assigning the values directly.
You wouldn''t use a pointer if you can do the assignment directly.
Declare a pointer, point it at some element of an array, dereference
it. Whew - seems like a lot of work when you can just: myArray[5]
=6.




然后就说myArray [5] = 6。


您是想询问指针,还是间接寻址?


DW



Then just say myArray[5] = 6.

Did you mean to ask just about pointers, or indirect addressing in general?

DW


这篇关于为什么指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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