Thread: const reference pass as argument?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    const reference pass as argument?

    Hey, I have class Timeprice, and the following code is
    Code:
    vector<Timeprice> data;
    void f(const Timeprice& tp);
    
    f(data[0]); // after executing operator[], it exit immediately, it didn't go into the function f() at all
    
    Timeprice aa = data[i];
    Timeprice& bb = data[i];
    f(aa);
    f(bb); //both call is respected
    can anybody explain to me why the call f(data[0]) didn't execute? Thanks!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    have you placed a breakpoint in the f function?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Sounds to me operator[] crashes. Probably because of a segfault or something similar.

    Edit: Didn't read the code properly. Makes no sense, unless the vector is completely empty...

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by EVOEx View Post
    Sounds to me operator[] crashes. Probably because of a segfault or something similar.

    Edit: Didn't read the code properly. Makes no sense, unless the vector is completely empty...
    Well, if you take the posted code at face value, the vector IS empty there.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by brewbuck View Post
    Well, if you take the posted code at face value, the vector IS empty there.
    Well, I do assume these are just snippets. Otherwise the other two should probably crash as well (even though it may not, obviously, as the behaviour is undefined).

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    For me it sounds as forgetting to press F11 once again after exiting the operator[]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  4. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM

Tags for this Thread