CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    Join Date
    May 2009
    Posts
    2,413

    Re: Modifying an existing array with a function

    Quote Originally Posted by GeoRanger View Post
    This is only my opinion and I know a lot of people, including the designers of the STL, disagree with me but I think references are vastly overused. In most cases, all they are doing is hiding a pointer so the programmer doesn't have to explicity use * and ->.
    Well, the designers of Java agree with you. In Java there's no by-reference parameter passing at all. It's always by-value (like in C). And the syntax is adapted to this so there's no notion of pointer either. References in C++ were introduced primarily to support operator overloading which is not available in Java.

    So the problem is you cannot have the versability of C++ without the complexity. You either must learn to live with it (using a coding standard helps), or you must switch to something simpler like Java.
    Last edited by nuzzle; March 29th, 2013 at 03:58 AM.

  2. #17
    Join Date
    May 2009
    Posts
    2,413

    Re: Modifying an existing array with a function

    Quote Originally Posted by fiodis View Post
    But I didn't know about static memory. Why is it called "static"? I can still change the value of variables in static memory, right?
    There are 4 kinds of different memory in C++: automatic (stack), dynamic (heap), static and thread (as of C++ 11).

    What separates these is the property of duration, that is the longevity of entities stored in each kind of memory. An entity in static memory stays for the whole of a program. I guess that's why it's called static. It sticks around from start to finish. It's always there all the time.
    Last edited by nuzzle; March 29th, 2013 at 05:27 AM.

  3. #18
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Modifying an existing array with a function

    Quote Originally Posted by nuzzle View Post
    In Java there's no by-reference parameter passing at all. It's always by-value (like in C).
    actually, one could say the same thing in C++: one can think at "pass-by-reference" simply as the process of initializing a variable of reference type. This way of thinking is especially useful given the proliferation of "references" due to c++11 ( r-value references, universal references, smart pointers, etc... ). Everything is "passed-by-value" ( or better, copy-initialized (*) ) and usual overloading/deduction/conversion rules are applied.

    I think this is a better mindset which reflects all the possible (arbitrarily many) ways in which you can model the idea of a variable holding a "reference" to something, depending on the specific guarantees/semantics one's looking for ( ownership, validity, mutability, etc... ).

    (*) in fact, references are not objects, hence have no "value" to "pass".

  4. #19
    Join Date
    May 2009
    Posts
    2,413

    Re: Modifying an existing array with a function

    Quote Originally Posted by superbonzo View Post
    actually, one could say the same thing in C++: one can think at "pass-by-reference" simply as the process of initializing a variable of reference type.
    Maybe it can be advantageous to think about it that way but that doesn't change the fact that C++ supports both major parameter passing modes: by-value and by-reference. The complexity due to this is still there regardless of how you think about it.

    These principally different modes are also useful when contrasting languages; Fortran has by-reference only, C has by-value only, Java has by-value only, Pascal has both, C++ has both, etcetera. With that basic knowledge in mind it's much easier to grasp the details for a specific language and paint a picture of how it "works" in each case.

  5. #20
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Modifying an existing array with a function

    Quote Originally Posted by nuzzle View Post
    These principally different modes are also useful when contrasting languages
    this is true, but is it really worth it ? consider the OP's doubts and georanger's reference aversion; I think both come from the fact that when they look at a code like "f(T& x)" they view the "&" as a syntactical intruder used to tell the compiler to avoid the copy of "x". This spurious semantics is unnecessary and wrong; IMO, it's much better to think the "&" as a type modifier as in the pointer case "T*", rather than a sort of variable modifier.

    Quote Originally Posted by nuzzle View Post
    C has by-value only, Java has by-value only, Pascal has both, C++ has both, etcetera.
    so, you claim that C++ supports pass-by-reference and C does not, because in the former you can write "f(T& x);f(x)" whereas in the latter you write "f(T* x);f(&x)", does just adding one less "&" justifies such a claim ? maybe, but similar reasonings could be said for most languages as well, beacuse most languages allow to express the ideas of copying vs referencing things, in one way or the other. For this reason I doubt the actual pedagogical effectiveness of the very idea of "passing modes" ...

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured