reference
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: reference

  1. #1
    Join Date
    Feb 2011
    Posts
    32

    reference

    What can i say about these two's differences ?

    reference opeartor[](size s);
    const_reference opeartor[](size s);


    Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,345

    Re: reference

    One returns a reference, the other returns a const_reference

    Actually, these are probably member functions, and the one that returns a const_reference probably should be declared const. Also, it is operator, not opeartor.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2011
    Posts
    32

    Re: reference

    Thank you, i copied and pasted without rereading them. Now I fix them,
    reference operator[](size_type s);
    const_reference operator[](size_type s);
    I guess I made a wrong question, mine should have been

    int & func( int arg);
    const int & func(int arg);

    This simple! But i can only imagine that the return types between both are different, and it's vague to me that the const-ness in the latter is seemingly redundant (as for what sake one needs to change the returned value that is...already returned)
    ??????????

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,345

    Re: reference

    Quote Originally Posted by Lucassergei
    But i can only imagine that the return types between both are different, and it's vague to me that the const-ness in the latter is seemingly redundant (as for what sake one needs to change the returned value that is...already returned)
    So that you can write stuff such as:
    Code:
    x.func(1) = 2;
    or in the case of operator[],
    Code:
    x[1] = 2;
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center