Thread: Constructor problem in list of classes

  1. #16
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    Those two functions are used to update the positions due to the effects of gravity, and "wrap" the star's position if it goes off screen. I was wondering though, when dereferencing the iterator, it returns by value right? So it actually would be faster if I modified the functions to accept iterators as parameters, rather than references to the object itself. Because the way I see it, even though I'm passing a reference to the function, by dereferencing the iterator I'm losing a whole lot of time since it has to return a copy of the value...no?

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by G4B3
    when dereferencing the iterator, it returns by value right?
    Not for an iterator to non-const, otherwise one cannot modify the object through the iterator. It would have to return by reference.

    Quote Originally Posted by G4B3
    So it actually would be faster if I modified the functions to accept iterators as parameters, rather than references to the object itself.
    It would make sense to use iterator parameters if you want to operate on a range, e.g., one parameter is an iterator to the start of the range, the other is an iterator to the end of the range. As an optimisation by itself it is probably a bad idea, since you would be unnecessarily requiring the use of iterators.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by G4B3
    when dereferencing the iterator, it returns by value right?
    Not for an iterator to non-const, otherwise one cannot modify the object through the iterator. It would have to return by reference.

    Quote Originally Posted by G4B3
    So it actually would be faster if I modified the functions to accept iterators as parameters, rather than references to the object itself.
    It would make sense to use iterator parameters if you want to operate on a range, e.g., one parameter is an iterator to the start of the range, the other is an iterator to the end of the range. As an optimisation by itself it is probably a bad idea, since you would be unnecessarily requiring the use of iterators.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM