Explain copy operation...

This is a discussion on Explain copy operation... within the C++ Programming forums, part of the General Programming Boards category; Im using a book that quote this as a copy operation used to return a reference as a value, but ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    30

    Question Explain copy operation...

    Im using a book that quote this as a copy operation used to return a reference as a value, but i dont understand how it works, and how i would be used...
    Code:
    TimeOfDay createTimeOfDay (int hours, int minutes) {
              TimeOfDay timeOfDay (hours, minutes);
                        return timeOfDay;}
    Thanks!

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It doesn't return a "reference as a value". Something either returns by value, or by reference. That returns by value.

    TimeOfDay timeOfDay(hours, minutes); // construct a TimeOfDay object with given paremeters
    return timeOfDay; // return a TimeOfDay, does exactly the same thing as return TimeOfDay(timeOfDay). That is, it calls the copy constructor of TimeOfDay with timeOfDay as the parameter.

    Note that this would be easier to read if the name of the class and the instance of the class didn't vary by capitailzation.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    30
    but how would it be used, and would the results be the value of hours and minutes??

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    That function is pretty useless... it does the exact same thing as a copy constructor, only slower.

    The result wold be a single instance of class/struct TimeOfDay, which probably has it's own methods for reading the value of the hours and minutes.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying constant amount of data
    By TriKri in forum C++ Programming
    Replies: 16
    Last Post: 07-12-2008, 06:32 AM
  2. Copy constructor question
    By BigFish21 in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2008, 12:18 PM
  3. LinkedList operator= and deep copy
    By sethjackson in forum C++ Programming
    Replies: 11
    Last Post: 02-28-2008, 11:54 AM
  4. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  5. open directory and copy files
    By 5n4k3 in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2003, 09:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21