我来自Java背景,已经开始使用C++中的对象。但我想到的一件事是,人们经常使用指向对象的指针,而不是对象本身,例如这样的声明:

Object *myObject = new Object;

而不是:

Object myObject;

或者不用函数,让我们说testFunc(),如下所示:

myObject.testFunc();

我们必须写下:

myObject->testFunc();

但我不明白我们为什么要这样做。我认为这与效率和速度有关,因为我们可以直接访问内存地址。我说得对吗?


当前回答

这已经详细讨论过了,但在Java中,一切都是指针。它不区分堆栈和堆分配(所有对象都在堆上分配),因此您不会意识到您正在使用指针。在C++中,可以根据内存需求将两者混合使用。C++中的性能和内存使用更具确定性(duh)。

其他回答

使用指针的一个原因是与C函数接口。另一个原因是节省内存;例如:不要将包含大量数据且具有处理器密集型复制构造函数的对象传递给函数,只需将指针传递给该对象即可节省内存和速度,尤其是在循环中,但在这种情况下引用会更好,除非您使用的是C样式数组。

“需要是发明之母。”我想指出的最重要的区别是我自己编码经验的结果。有时需要将对象传递给函数。在这种情况下,如果您的对象是一个非常大的类,那么将其作为对象传递将复制其状态(您可能不希望..AND可以是big OVERHEAD),从而导致复制对象的开销。而指针是固定的4字节大小(假设为32位)。上面已经提到了其他原因。。。

C++中对象指针的关键优势是允许同一超类的指针的多态数组和映射。例如,它允许将长尾鹦鹉、鸡、知更鸟、鸵鸟等放在鸟的阵列中。

此外,动态分配的对象更灵活,可以使用HEAP内存,而本地分配的对象将使用STACK内存,除非它是静态的。堆栈上有大型对象,尤其是使用递归时,无疑会导致堆栈溢出。

假设你有一个包含类B的类A。当你想在类A之外调用类B的某个函数时,你只需要获得一个指向这个类的指针,你可以做任何你想做的事情,它也会改变类A中类B的上下文

但要小心动态对象

这个问题有很多很好的答案,包括前向声明、多态性等重要用例,但我觉得你的问题的“灵魂”部分没有得到回答,即Java和C++中不同的语法意味着什么。

让我们来比较一下这两种语言的情况:

Java语言:

Object object1 = new Object(); //A new object is allocated by Java
Object object2 = new Object(); //Another new object is allocated by Java

object1 = object2; 
//object1 now points to the object originally allocated for object2
//The object originally allocated for object1 is now "dead" - nothing points to it, so it
//will be reclaimed by the Garbage Collector.
//If either object1 or object2 is changed, the change will be reflected to the other

与此最接近的等效值为:

C++:

Object * object1 = new Object(); //A new object is allocated on the heap
Object * object2 = new Object(); //Another new object is allocated on the heap
delete object1;
//Since C++ does not have a garbage collector, if we don't do that, the next line would 
//cause a "memory leak", i.e. a piece of claimed memory that the app cannot use 
//and that we have no way to reclaim...

object1 = object2; //Same as Java, object1 points to object2.

让我们看看另一种C++方式:

Object object1; //A new object is allocated on the STACK
Object object2; //Another new object is allocated on the STACK
object1 = object2;//!!!! This is different! The CONTENTS of object2 are COPIED onto object1,
//using the "copy assignment operator", the definition of operator =.
//But, the two objects are still different. Change one, the other remains unchanged.
//Also, the objects get automatically destroyed once the function returns...

最好的方法是——或多或少——Java(隐式)处理指向对象的指针,而C++可以处理指向对象或对象本身的指针。这是有例外的——例如,如果您声明Java“原始”类型,它们是复制的实际值,而不是指针。所以

Java语言:

int object1; //An integer is allocated on the stack.
int object2; //Another integer is allocated on the stack.
object1 = object2; //The value of object2 is copied to object1.

也就是说,使用指针不一定是正确或错误的处理方式;然而,其他答案已经令人满意地涵盖了这一点。不过,总的想法是,在C++中,您可以对对象的生存期以及它们将生存的位置进行更多的控制。

重点是——Object*Object=newObject()构造实际上最接近典型的Java(或C#)语义。