Thread: About passing reference question

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    About passing reference question

    I have a question about passing reference
    I have a function prototype like void Queue::enqueue(Const Obj& o);
    The first case, I try to pass a temp Obj instance to a function
    but compile error occurs
    Code:
    Queue q;
    q.enqueue(Obj(10));
    I got compile error like following
    g++ -g -Wall -c main.cpp -o main.o
    main.cpp: In function ‘int main()’:
    main.cpp:17: error: no matching function for call to ‘Queue::enqueue(Obj)’
    queue.h:29: note: candidates are: void Queue::enqueue(Obj&)

    Then I try to modify the code as following
    Code:
    Queue q;
    Obj o(10);
    q.enqueue(o);
    And this one can be compiled
    Could anyone explain what's the difference between these two?
    Thx in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You apparently did not declare enqueue to take the Obj argument by const reference. You declared it to take the Obj argument by non-const reference instead. Besides the obvious difference that for one you can modify the object through the difference, the difference is that with a const reference parameter, you can pass a temporary object, but with a non-const reference parameter you cannot.
    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. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Thx a lot. I am embarrassed I didn't figure out I miss the const specifier.
    However, I didn't know passing a temporary object to a non const reference is illegal while to const one is valid. Learned a lesson. Thx

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, here's another treat for you:
    Code:
    void Queue::enqueue(Obj&& obj) { /* ... */ }
    
    Queue q;
    q.enqueue(Obj(10));
    Be sure to enable C++0x mode.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Is that a reference to a reference???
    What would be the point of that?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It isn't, It's an r-value reference. A non-const reference that binds to temporaries (not not lvalues unless you use std::move). See wikipedia for more info.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  2. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  3. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  4. Passing a 2d array by Reference
    By loko in forum C Programming
    Replies: 8
    Last Post: 07-23-2005, 06:19 AM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM