const reference pass as argument?

This is a discussion on const reference pass as argument? within the C++ Programming forums, part of the General Programming Boards category; Hey, I have class Timeprice, and the following code is Code: vector<Timeprice> data; void f(const Timeprice& tp); f(data[0]); // after ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    have you placed a breakpoint in the f function?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    For me it sounds as forgetting to press F11 once again after exiting the operator[]
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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


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