c+ - Pourquoi utiliser un pointeur plutôt que l'objet lui-même

Mots clés : c++c++11pointersc++-faqc++

meilleur 3 Réponses c+ - Pourquoi utiliser un pointeur plutôt que l'objet lui-même

vote vote

93

class Base { ... }; class Derived : public Base { ... };  void fun(Base b) { ... } void gun(Base* b) { ... } void hun(Base& b) { ... }  Derived d; fun(d);    // oops, all Derived parts silently "sliced" off gun(&d);   // OK, a Derived object IS-A Base object hun(d);    // also OK, reference also doesn't slice 
Base b; fun(b);  // copies b, potentially expensive  gun(&b); // takes a pointer to b, no copying hun(b);  // regular syntax, behaves as a pointer 
{     auto b = new Base;     ...       // oops, if an exception is thrown, destructor not called!     delete b; } 
{     auto b = std::make_unique<Base>();     ...       // OK, now exception safe } 
vote vote

88

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

79

{     std::string s; } // s is destroyed here 
{     std::string* s = new std::string; } delete s; // destructor called 
char buffer[100]; std::cin >> buffer; // bad input = buffer overflow 
int * pointer; std::cout << "How many items do you need?"; std::cin >> n; pointer = new int[n]; 
int arr[5] = {1, 2, 3, 4, 5}; // initialization  int arr[] = {1, 2, 3, 4, 5}; // The standard dictates that the size of the array                              // be given by the amount of members in the initializer   arr = { 1, 2, 3, 4, 5 }; // ERROR 
std::vector<Base*> vector; vector.push_back(&square); vector.push_back(&triangle); for (auto& e : vector) {      auto test = dynamic_cast<Triangle*>(e); // I only care about triangles      if (!test) // not a triangle         e.GenericFunction();      else         e.TriangleOnlyMagic(); } 

Questions similaires