Constructor problem in list of classes

This is a discussion on Constructor problem in list of classes within the C++ Programming forums, part of the General Programming Boards category; Those two functions are used to update the positions due to the effects of gravity, and "wrap" the star's position ...

  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
    21,588
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,588
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
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, 02: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, 01:46 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05: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

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