reference doesn't work?

This is a discussion on reference doesn't work? within the C++ Programming forums, part of the General Programming Boards category; >> why can't I do with a reference? Why do you want to do it with a reference? What do ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> why can't I do with a reference?
    Why do you want to do it with a reference? What do you think will happen that will make things better? What would be the point? Why? Your code is exactly the same whether you use a reference or a pointer except for the declaration of q which uses & or *. Everything else is the same. Why do you think a reference is "better"?

    >> You can and should do it with a reference.
    No. You should use a pointer. There is no reason to use a reference to a pointer to point to an array. Just use a regular pointer.

  2. #17
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    Me?

    Just try to understand the difference between reference and pointer. I don't want to step into an error area without knowing beforehand...

    Quote Originally Posted by siavoshkc
    Why you are insisting in using a reference?

  3. #18
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    Come on: there're plenty of codes where the argument of a function takes the form like this:
    Code:
     
    
    double f(/*some class of array type*/ &x) {
    }
    Why do they use reference instead of a pointer?


    Quote Originally Posted by Daved
    >> why can't I do with a reference?
    Why do you want to do it with a reference? What do you think will happen that will make things better? What would be the point? Why? Your code is exactly the same whether you use a reference or a pointer except for the declaration of q which uses & or *. Everything else is the same. Why do you think a reference is "better"?

    >> You can and should do it with a reference.
    No. You should use a pointer. There is no reason to use a reference to a pointer to point to an array. Just use a regular pointer.

  4. #19
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,406
    Quote Originally Posted by Daved
    No. You should use a pointer. There is no reason to use a reference to a pointer to point to an array. Just use a regular pointer.
    Oh I don't know. I'm just quoting from the man who invented the language. What does he know?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Come on: there're plenty of codes where the argument of a function takes the form like this:
    >> double f(/*some class of array type*/ &x)
    No, there isn't. That code does not happen because you cannot have a reference to an array. I think you are confused.

    Again, if a reference did work, your code would be the same as it is with the pointer, so the question is what benefit would a reference have? Are you just trying to follow a rule like the one Mario F. posted? Why do you think a reference would work?

    >> Oh I don't know. I'm just quoting from the man who invented the language. What does he know?
    You are misusing that quote. It doesn't apply in this situation.

  6. #21
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    For the codes that have already used reference argument, do you think I have to change them to pointers?
    If reference cannot apply to array the same as a pointer does, then I think I'm confused...


    Quote Originally Posted by Daved
    >> Come on: there're plenty of codes where the argument of a function takes the form like this:
    >> double f(/*some class of array type*/ &x)
    No, there isn't. That code does not happen because you cannot have a reference to an array. I think you are confused.

    Again, if a reference did work, your code would be the same as it is with the pointer, so the question is what benefit would a reference have? Are you just trying to follow a rule like the one Mario F. posted? Why do you think a reference would work?

    >> Oh I don't know. I'm just quoting from the man who invented the language. What does he know?
    You are misusing that quote. It doesn't apply in this situation.

  7. #22
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Read my edit?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What code that uses a reference argument? Can you give an example? Any code that takes a reference should not be expecting an array, so trying to pass an array to it shouldn't make sense anyway.

  9. #24
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,406
    Quote Originally Posted by asmileguo
    Just try to understand the difference between reference and pointer. I don't want to step into an error area without knowing beforehand...
    Your confusion arised from the fact you forgot a pointer is also a type. A compund type to be more precise. A pointer type is defined in terms of another type.

    When you see int* foo, you may lead to phrase that foo "is a pointer to an int". Which basically is correct. However, until you get comfortable enough about pointers, you will probably do better in phrasing the above as "foo is of type pointer to int." That way you will remember that "pointer" is part of foo's type.

    So to create a reference to it, you simply do as you would normally. int* &bar = foo.

    If this is of any use, it is debatable. Daved does have a point. Most people who advocate references over pointers, do to.

    Choose for yourself: http://www.parashift.com/c++-faq-lite/references.html
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Mario F., I advocate references over pointers as well. That doesn't apply in this case.
    Last edited by Daved; 08-09-2006 at 12:01 PM.

  11. #26
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,406
    Quote Originally Posted by Daved
    You are misusing that quote. It doesn't apply in this situation.
    An array is a type. Give me one good reason why references shouldn't be created for arrays. It makes no sense.

    A reference IS the object it points to. Contrary to a pointer. You are simply giving your array another name, just like you do with other types.

    help me understand your point
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #27
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    I found be writing as
    Code:
    int* &bar
    is much clearer than
    Code:
    int  *&bar
    I also notice in Stroustrop's book, he always write in the first way. Can all compilers handle that without confusion?


    Quote Originally Posted by Mario F.
    So to create a reference to it, you simply do as you would normally. int* &bar = foo.


  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Give me one good reason why references shouldn't be created for arrays. It makes no sense.

    I don't know what you are talking about. Are you saying that a reference to an array should be legalized? Or are you saying that one should prefer a reference to a pointer to an array over a pointer to an array?
    Last edited by Daved; 08-09-2006 at 12:05 PM.

  14. #29
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,406
    EDIT: deleted previous post for sanity sake. You actually gave me a good reason.

    "An array needs a pointer to be manipulated, so why create a reference." It was something like this, wasn't it? I don't understand why you removed it.
    Last edited by Mario F.; 08-09-2006 at 12:08 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #30
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    asmileguo, there is no reason to use int* &bar, int *&bar, or int*& bar. Use int* bar. If you don't understand why, please say so. If you have a particular situation that you think it is better, please post an example, because in all the code in this thread there is no reason to use a reference to a pointer. I think Mario F now agrees with me, too.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  5. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 12:00 AM

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