Thread: 'Passing by Refrence for Efficiency', Copy Constructors?

  1. #1
    #junkie
    Join Date
    Oct 2004
    Posts
    240

    Post 'Passing by Refrence for Efficiency', Copy Constructors?

    Ok, im reading Sams Teach yourself C++ in 21 days, overall i like it. But there is one thing i am highly confused by.

    At the moment i am reading about passing by refrences, passing objects atm. And in the code example i am about to copy to you i do not understand what the copy constructor is for. That and the refrence opterator behind the class name (eg below)
    Code:
    public: 
        SimpleCat();  // original constructor
        SimpleCat(SimpleCat&); // copy constructor
        ~SimpleCat(); // obviously deconstructor
    So can someone help? Thanks!
    Code:
    1: //Listing 9.12
    2: // Passing references to objects
    3:
    4:   #include <iostream.h>
    5:
    6:   class SimpleCat
    7:   {
    8:   public:
    9:           SimpleCat();
    10:           SimpleCat(SimpleCat&);
    11:           ~SimpleCat();
    12:
    13:           int GetAge() const { return itsAge; }
    14:           void SetAge(int age) { itsAge = age; }
    15:
    16:   private:
    17:           int itsAge;
    18:      };
    19:
    20:      SimpleCat::SimpleCat()
    21:      {
    22:             cout << "Simple Cat Constructor...\n";
    23:             itsAge = 1;
    24:      }
    25:
    26:      SimpleCat::SimpleCat(SimpleCat&)
    27:      {
    28:             cout << "Simple Cat Copy Constructor...\n";
    29:      }
    30:
    31:      SimpleCat::~SimpleCat()
    32:      {
    33:             cout << "Simple Cat Destructor...\n";
    34:      }
    35:
    36:      const     SimpleCat & FunctionTwo (const SimpleCat & theCat);
    37:
    38:      int main()
    39:      {
    40:             cout << "Making a cat...\n";
    41:             SimpleCat Frisky;
    42:             cout << "Frisky is " << Frisky.GetAge() << " years old\n";
    43:             int age = 5;
    44:             Frisky.SetAge(age);
    45:             cout << "Frisky is " << Frisky.GetAge() << " years old\n";
    46:             cout << "Calling FunctionTwo...\n";
    47:             FunctionTwo(Frisky);
    48:             cout << "Frisky is " << Frisky.GetAge() << " years old\n";
    49:     return 0;
    50:      }
    51:
    52:      // functionTwo, passes a ref to a const object
    53:      const SimpleCat & FunctionTwo (const SimpleCat & theCat)
    54:      {
    55:                      cout << "Function Two. Returning...\n";
    56:                      cout << "Frisky is now " << theCat.GetAge();
    57:                      cout << " years old \n";
    58:                      // theCat.SetAge(8);   const!
    59:                      return theCat;
    60: }
    
    Output: Making a cat...
    Simple Cat constructor...
    Frisky is 1 years old
    Frisky is 5 years old
    Calling FunctionTwo...
    FunctionTwo. Returning...
    Frisky is now 5 years old
    Frisky is 5 years old
    Simple Cat Destructor...
    so if you can, please expain to me the reason for the copy constructor existing, and what it is lol. Thanks! (that and the refrence operator behind SimpleCat (ie "(SimpleCat&)") thanks!
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Since the output coded into the copy constructor isn't in the output, the copy constructor isn't used in the code. However, every class must have a copy constructor. If you don't provide one the compiler will. The compiler provided copy constructor may not do what you want it to, so the best advice is you create your own for every class you create. The author did that. In my opinion it would have been better to include a simple copy of the data member, so at least it would be functional or to leave it out completely, but there's nothing wrong with the way it was done as in "no harm, no foul".

    The & operator is indicating that you are passing a reference to the function, not a copy of the objects data. Because you don't want the data in the passed object to be changed it is declared as a const reference, rather than just a plain reference. Given the simple nature of the SimpleCat class members, a copy by value approach would have worked just as well in this instance, but classes can be very large, and a useful rule is to pass by reference except for primitive types like int, double, char, etc.

    In the function call to FunctionTwo, using the objects name is adequate as the compiler will change the name of the object into a reference to the object. You don't have to create a reference and pass it yourself, though you can if you want to.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    The reason the author has the copy constructor there is to show you the difference between pass by reference and pass by value. Pass by reference does not copy the object, it merely passes the function a reference to the object so the function can just refer to the original object passed to it. If you used pass by value, then the copy constructor would be called to make a copy of the object, and the function uses the copy. That can often be inefficient, which is why pass by reference is common. The existence of the copy constructor in that code is simply to show that it is not being called in this case.

    The '&' in this case just indicates that you are accepting a reference, as elad indicated.


    Actually, the copy constructor the way it was posted is incorrect (or at least not correct by convention). The data is never copied. In this case, the only data is the age, but it isn't copied in the copy constructor. Once you define a copy constructor, you better define it correctly, because the compiler won't fix it for you.

    In this case, it is more appropriate to not define a copy constructor, since the compiler generated one would have been completely appropriate. Of course, the author is simply trying to show you what a copy constructor is and when it is called, so it is only an example and it shouldn't be worried about. The fact that the output from the copy constructor is not displayed shows that it is never called, and no copy has taken place.

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    while we are on the topic of copy construction.. is there a way to know if the copy constructor your wrote actually works...?? I have wrote a couple of copy constructors.. program runs fine.. I'm just not sure if the copy constructor is working.. or if it's just doing nothing.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is there a way to know if the copy constructor your wrote actually works...??
    Unless you perform operations that could fail without throwing an exception and don't check them then if execution gets past the constructor, it worked. Otherwise you'll have a catastrophic failure either through an uncaught exception or a program halt. If you do something like call fopen in the constructor and don't check for failure then you won't know if construction succeeded until you try to use the null pointer and all hell breaks loose.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 03-18-2006, 09:14 AM
  2. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  3. passing constructor data to other constructors
    By eth0 in forum C++ Programming
    Replies: 9
    Last Post: 05-21-2004, 03:20 AM
  4. passing by refrence isn't allowed in C?
    By Lynux-Penguin in forum C Programming
    Replies: 19
    Last Post: 10-10-2003, 06:46 AM
  5. passing by refrence problems
    By rippascal in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2002, 10:04 PM