我知道这是一个非常基本的问题,但在用高级语言编写了几个项目之后,我刚刚开始了一些基本的c++编程。
基本上我有三个问题:
为什么使用指针而不是普通变量? 我应该在何时何地使用指针? 如何在数组中使用指针?
我知道这是一个非常基本的问题,但在用高级语言编写了几个项目之后,我刚刚开始了一些基本的c++编程。
基本上我有三个问题:
为什么使用指针而不是普通变量? 我应该在何时何地使用指针? 如何在数组中使用指针?
当前回答
在c++中,如果你想使用子类型多态性,你必须使用指针。请参阅这篇文章:没有指针的c++多态性。
真的,仔细想想,这是有道理的。当您使用子类型多态性时,最终,您不知道将调用哪个类或子类的方法实现,因为您不知道实际的类是什么。
使用变量保存未知类的对象的想法与c++在堆栈上存储对象的默认(非指针)模式不兼容,在堆栈中分配的空间量直接对应于类。注意:如果一个类有5个而不是3个实例字段,则需要分配更多的空间。
请注意,如果您使用'&'通过引用传递参数,在幕后仍然涉及到间接(即指针)。'&'只是语法糖,它(1)省去了使用指针语法的麻烦,(2)允许编译器更严格(例如禁止空指针)。
其他回答
使用指针的一个原因是可以在被调用的函数中修改变量或对象。
在c++中,使用引用比使用指针更好。虽然引用本质上是指针,但c++在某种程度上隐藏了这一事实,让它看起来好像是通过值传递的。这使得更改调用函数接收值的方式变得很容易,而不必修改传递值的语义。
考虑以下例子:
使用引用:
public void doSomething()
{
int i = 10;
doSomethingElse(i); // passes i by references since doSomethingElse() receives it
// by reference, but the syntax makes it appear as if i is passed
// by value
}
public void doSomethingElse(int& i) // receives i as a reference
{
cout << i << endl;
}
使用指针:
public void doSomething()
{
int i = 10;
doSomethingElse(&i);
}
public void doSomethingElse(int* i)
{
cout << *i << endl;
}
以下是我的答案,我不会保证自己是专家,但我在我正在编写的一个库中发现了很棒的指针。在这个库中(它是一个带有OpenGL的图形API:-)),你可以创建一个带有顶点对象的三角形。draw方法取这些三角形对象,然后。根据我创建的顶点对象绘制它们。嗯,没关系。
但是,如果我改变顶点坐标呢?移动它或者在顶点类中使用moveX() ?好吧,现在我必须更新三角形,添加更多的方法和性能是浪费的,因为我必须更新三角形每次顶点移动。虽然不是什么大事,但也没那么好。
现在,如果我有一个有大量顶点和三角形的网格,网格在旋转,移动,等等。我必须更新使用这些顶点的每个三角形,可能在场景中的每个三角形,因为我不知道哪个使用哪个顶点。这需要大量的计算机,如果我在一个景观上有几个网格,天哪!我遇到麻烦了,因为我更新了每个三角形,几乎每一帧,因为这些顶点一直在变化!
对于指针,你不需要更新三角形。
If I had three *Vertex objects per triangle class, not only am i saving room because a zillion triangles don't have three vertex objects which are large themselves, but also these pointers will always point to the Vertices they are meant to, no matter how often the vertices change. Since the pointers still point to the same vertex, the triangles don't change, and the update process is easier to handle. If I confused you, I wouldn't doubt it, I don't pretend to be an expert, just throwing my two cents into the discussion.
让我也试着回答这个问题。
指针类似于引用。换句话说,它们不是副本,而是引用原始值的一种方式。
首先,在处理嵌入式硬件时,通常必须大量使用指针。也许你需要切换数字IO引脚的状态。也许你正在处理一个中断,需要在一个特定的位置存储一个值。你懂的。但是,如果您不直接处理硬件,只是想知道使用哪种类型,请继续阅读。
为什么使用指针而不是普通变量?当你在处理复杂的类型时,比如类、结构和数组,答案就会变得更清楚。如果你要使用一个普通的变量,你可能最终会生成一个副本(编译器足够聪明,在某些情况下可以防止这种情况,c++ 11也有帮助,但我们现在不讨论这个问题)。
现在如果你想修改原始值会发生什么?你可以使用这样的代码:
MyType a; //let's ignore what MyType actually is right now.
a = modify(a);
这可以很好地工作,如果你不知道为什么要使用指针,你就不应该使用它们。当心“他们可能更快”的理由。运行您自己的测试,如果它们确实更快,那么就使用它们。
但是,假设您正在解决一个需要分配内存的问题。当你分配内存时,你需要释放它。内存分配可能成功,也可能不成功。这就是指针有用的地方——它们允许您测试已分配对象的存在,并允许您通过取消引用指针来访问内存所分配的对象。
MyType *p = NULL; //empty pointer
if(p)
{
//we never reach here, because the pointer points to nothing
}
//now, let's allocate some memory
p = new MyType[50000];
if(p) //if the memory was allocated, this test will pass
{
//we can do something with our allocated array
for(size_t i=0; i!=50000; i++)
{
MyType &v = *(p+i); //get a reference to the ith object
//do something with it
//...
}
delete[] p; //we're done. de-allocate the memory
}
这就是为什么你要使用指针的关键——引用假设你引用的元素已经存在。而指针则不然。
使用指针(或者至少最终不得不处理指针)的另一个原因是因为它们是在引用之前就存在的数据类型。因此,如果你最终使用库来做你知道它们更擅长的事情,你会发现很多库都在到处使用指针,这仅仅是因为它们存在的时间长(其中很多是在c++之前编写的)。
如果你不使用任何库,你可以用这样一种方式来设计你的代码,你可以远离指针,但鉴于指针是语言的基本类型之一,你越快适应使用它们,你的c++技能就越可移植。
From a maintainability point of view, I should also mention that when you do use pointers, you either have to test for their validity and handle the case when they're not valid, or, just assume they are valid and accept the fact that your program will crash or worse WHEN that assumption is broken. Put another way, your choice with pointers is to either introduce code complexity or more maintenance effort when something breaks and you're trying to track down a bug that belongs to a whole class of errors that pointers introduce, like memory corruption.
因此,如果您控制所有代码,请远离指针,而使用引用,尽可能将它们保持为const。这将迫使您考虑对象的生命周期,并最终使您的代码更容易理解。
只要记住这个区别:引用本质上是一个有效的指针。指针并不总是有效的。
所以我是说不可能创建无效引用吗?不。这是完全可能的,因为c++几乎可以让你做任何事情。只是在无意中更难做到,你会惊讶于有多少bug是无意的:)
因为到处复制大对象会浪费时间和内存。
Pointers allow you to refer to the same space in memory from multiple locations. This means that you can update memory in one location and the change can be seen from another location in your program. You will also save space by being able to share components in your data structures. You should use pointers any place where you need to obtain and pass around the address to a specific spot in memory. You can also use pointers to navigate arrays: An array is a block of contiguous memory that has been allocated with a specific type. The name of the array contains the value of the starting spot of the array. When you add 1, that takes you to the second spot. This allows you to write loops that increment a pointer that slides down the array without having an explicit counter for use in accessing the array.
下面是一个C语言的例子:
char hello[] = "hello";
char *p = hello;
while (*p)
{
*p += 1; // increase the character by one
p += 1; // move to the next spot
}
printf(hello);
打印
ifmmp
因为它取每个字符的值并加1。