Sei sulla pagina 1di 314
www.freeOnlineBooks.tk part of www.asadali.tk Download Books for Free Read Books Online C++ from the Ground Up Rixcamre. Copy constructors do not affect assignment operations. The capy constructor Is called when one object initializes another. The most common form of copy constructor is shown here: classname (const classname &obj) { J] body of constructor } Here, obj is a reference to an object that is being used to initialize another object. For example, assuming a class called myelass, and y as an abject of type myelass, then the following statements would invoke the myclass copy constructor: myclass x = y; // y explicitly initializing x funel (y); // ¥ passed as a parameter y= func2(); // y receiving a returned object In the first two cases, a reference to y would be passed to the copy constructor. In the third, a reference to the object returned by fume2( ) would be passed to the copy constructor. To fully explore the value of copy constructors, let's see how they impact each of the three situations to which they apply. Copy Constructors and Parameters When an object is passed to a function as an argument, a copy of that object is made. Ifa copy constructor exists, the copy constructor is called to make the copy. Here is a program that uses a copy constructor to properly handle objects of type myclass when they are passed to a function. (This is a corrected version of the incorrect program shown earlier in this chapter.) // Use a copy constructor to construct a parameter. #include #include using namespace std; class myclass ( int *p; public: myclass(int i}; // normal constructor myclass(const myclass &ob); // copy constructor ~myclass(}; int getval() { return *p; } // Copy constructor. A Closer Look at Classes myclass::myclass(const myclass &obj) { p = new int; *p = *obj.p; // copy value cout << “Copy constructor called.\n‘; t // Normal Constructor. myclass::myclass(int i) { gout << "Allocating pin"; p = new int; "p= i } myclass: :~myclass() { cout << "Freeing p\n"; delete p; + // This function takes one object parameter. void display(myclass ob) { cout << ob.getval() << '"\n'; 1 int main() { myclass a{10); display(a); return 0; This program displays the following output: Allocating p Copy constructor called. 12 10 Freeing p Freeing p Here is what occurs when the program is run: When a is created inside main( ), the normal constructor allocates memory and assigns the address of that memory to a.p. Next, a is passed to ob of display( ). When this occurs, the copy constructor is called, and a copy of a is created. The copy constructor allocates memory for the copy, and a pointer to that memory is assigned to the copy’s p member. Next, the value stored at the original object’s p is assigned to the memory pointed to by the copy’s p. Thus, the areas of memory pointed to by a.p and ob.p are separate and distinct, but the values

Potrebbero piacerti anche

  • CPPBG 2
    CPPBG 2
    Documento178 pagine
    CPPBG 2
    creative_asad3742
    Nessuna valutazione finora
  • CPPVC 51
    CPPVC 51
    Documento615 pagine
    CPPVC 51
    creative_asad3742
    Nessuna valutazione finora
  • Cppts 2
    Cppts 2
    Documento305 pagine
    Cppts 2
    creative_asad3742
    Nessuna valutazione finora
  • Cppts 1
    Cppts 1
    Documento254 pagine
    Cppts 1
    creative_asad3742
    Nessuna valutazione finora
  • Cppsams 1
    Cppsams 1
    Documento444 pagine
    Cppsams 1
    creative_asad3742
    Nessuna valutazione finora
  • Cppsams 2
    Cppsams 2
    Documento444 pagine
    Cppsams 2
    creative_asad3742
    Nessuna valutazione finora
  • Cppmath 2
    Cppmath 2
    Documento262 pagine
    Cppmath 2
    creative_asad3742
    Nessuna valutazione finora
  • Cppmath 1
    Cppmath 1
    Documento261 pagine
    Cppmath 1
    creative_asad3742
    Nessuna valutazione finora
  • CPPLP 1
    CPPLP 1
    Documento316 pagine
    CPPLP 1
    creative_asad3742
    Nessuna valutazione finora
  • CPPLP 2
    CPPLP 2
    Documento311 pagine
    CPPLP 2
    creative_asad3742
    Nessuna valutazione finora
  • Cppdisec 2
    Cppdisec 2
    Documento261 pagine
    Cppdisec 2
    creative_asad3742
    Nessuna valutazione finora
  • CPPGB 1
    CPPGB 1
    Documento313 pagine
    CPPGB 1
    creative_asad3742
    Nessuna valutazione finora
  • Cppdisec 1
    Cppdisec 1
    Documento261 pagine
    Cppdisec 1
    creative_asad3742
    Nessuna valutazione finora
  • CPPVC 52
    CPPVC 52
    Documento614 pagine
    CPPVC 52
    creative_asad3742
    Nessuna valutazione finora
  • Cppdisec 1
    Cppdisec 1
    Documento261 pagine
    Cppdisec 1
    creative_asad3742
    Nessuna valutazione finora