Sams Teach Yourself in 24 hours.....

This is a discussion on Sams Teach Yourself in 24 hours..... within the C++ Programming forums, part of the General Programming Boards category; The address. A reference is implemented the same as a pointer, it just has different rules....

  1. #16
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    The address.

    A reference is implemented the same as a pointer, it just has different rules.

  2. #17
    Registered User
    Join Date
    Aug 2003
    Posts
    17
    Another good book on the c++ language is the c++ all-ni-one deskreference for dummies. Its in the "for dummies" line of books (you know, the yellow and black ones) I have found it very helpful and an easy read

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Cat,

    So, a reference is implemented as some sort of constant pointer in such a way that you can't get at the address? And, all this talk of pass-by-reference is a hoax?

  4. #19
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Sometimes, sometimes not.

    References and pointers have a different interface, they have different syntax and different rules, but the underlying implementation is similar.

    It's not really a "hoax", they are two different ways of using the same underlying idea (passing the address of a variable). Each has its uses; because of the different syntax, references can do things you can't do with pointers, and vice versa.

    Also, although a reference is passed to and from functions like a pointer (by passing the address), within a function it takes up no additional space; it acts like an alias to the same memory location.
    Last edited by Cat; 08-21-2003 at 09:07 PM.

  5. #20
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It's not really a "hoax", they are two different ways of using the same underlying idea

    It seems to me either it's passed-by-value or it's not. A pointer is passed by value to a function, i.e. a copy is made for the function, and the function uses that copy. If a reference is also passed by value then a copy is made for the function, and it seems to me it has to take up some space. If a reference isn't copied, then a reference should be more efficient than a pointer since no copying is necessary.

  6. #21
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    But an address still needs to be passed. The function needs to know where the object is located. Unless the function is inlined, there will be only one binary version of the function, and if it's to operate multiple times, then it has to be able to accept a parameter. This parameter must be passed in the traditional fashion (usually by being pushed to the stack), and so the method is to push the address of a reference parameter.

    In fact, a pointer passed to a function or a reference passed to a function will likely generate identical code.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file
    By nevrax in forum C Programming
    Replies: 12
    Last Post: 04-23-2007, 06:18 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Time between Military Hours
    By StarOrbs in forum C++ Programming
    Replies: 18
    Last Post: 03-01-2005, 05:46 PM
  4. Sams Teach Yourself Game Programming in 24 Hours
    By Android in forum Game Programming
    Replies: 17
    Last Post: 06-08-2004, 10:20 PM
  5. Sams Teach Yourself ?
    By CAP in forum C Programming
    Replies: 4
    Last Post: 09-03-2002, 08:16 PM

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