About passing reference question

This is a discussion on About passing reference question within the C++ Programming forums, part of the General Programming Boards category; I have a question about passing reference I have a function prototype like void Queue::enqueue(Const Obj& o); The first case, ...

  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
    22,265
    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.
    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. #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
    Posts
    23,002
    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
    Posts
    23,002
    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, 11:04 AM
  3. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 11:37 AM
  4. Passing a 2d array by Reference
    By loko in forum C Programming
    Replies: 8
    Last Post: 07-23-2005, 07: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

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