Why should I use a pointer rather than the object itself?

后端 未结 22 1686
予麋鹿
予麋鹿 2020-11-21 23:26

I\'m coming from a Java background and have started working with objects in C++. But one thing that occurred to me is that people often use pointers to objects rather than t

22条回答
  •  难免孤独
    2020-11-22 00:12

    There are many excellent answers to this question, including the important use cases of forward declarations, polymorphism etc. but I feel a part of the "soul" of your question is not answered - namely what the different syntaxes mean across Java and C++.

    Let's examine the situation comparing the two languages:

    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
    

    The closest equivalent to this, is:

    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.
    

    Let's see the alternative C++ way:

    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...
    

    The best way to think of it is that -- more or less -- Java (implicitly) handles pointers to objects, while C++ may handle either pointers to objects, or the objects themselves. There are exceptions to this -- for example, if you declare Java "primitive" types, they are actual values that are copied, and not pointers. So,

    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.
    

    That said, using pointers is NOT necessarily either the correct or the wrong way to handle things; however other answers have covered that satisfactorily. The general idea though is that in C++ you have much more control on the lifetime of the objects, and on where they will live.

    Take home point -- the Object * object = new Object() construct is actually what is closest to typical Java (or C# for that matter) semantics.

提交回复
热议问题