Returning by reference

Hello

Like we pass arguments to function by reference, how do we return by reference?

Thanks
1
2
3
4
5
6
7
struct A
{
   A & operator =( const A & )
   {
      return ( *this );
   }
};
@vlad from moscow


Can you please explain with a short example?

Thanks
I showed you an example. If you need another example then you can consider the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
   std::string s;

   std::cout <<
      s.append( 1, 'H' ).append( 1, 'e' ).append( 1, 'l' ).append( 1, 'l' ).append( 1, 'o' ) 
      << std::endl;

//   std::cout << s << std::endl;
}


Usually functions return an object by reference when they get the same object by reference as an argument (as an lvalue).
Last edited on
Thanks for your help!
And i wanted to ask one more thing: I am new to C++ programming(but have covered most of the basics) and they are teaching me at the school with turbo 3.0. I know that it is not C++11 compilant and therefore don't know about many new library functions. Can you please suggest a site that offers such tutorials.

Thanks again
I advice to look through http://isocpp.org/
they are teaching me at the school with turbo 3.0. I know that it is not C++11 compilant

This isn't a C++11 issue. Turbo "C++" 3.0 predates the first C++ standard, C++98, by many years, which is why it can't even compile vlad's example.
> Can you please suggest a site that offers such tutorials.

This is pretty comprehensive: http://cppannotations.sourceforge.net/annotations/html/


> they are teaching me at the school with turbo 3.0.

Get a good compiler for yourself. There are several options; for instance:
http://sourceforge.net/projects/codeblocks/files/Binaries/12.11/Windows/codeblocks-12.11mingw-setup.exe
@JLBorges


Though I mentioned that my school teaches me using Turbo 3.0, I use Code::Blocks, which you just mentioned. I already know much from my very little experience that learning in turbo won't help me develop.

@Cubbi

I know that this isn't a C++11 issue, what I was asking vlad was that can he give me a site for learning programming according to C++11 standard which he already provided.

Thanks to all
Last edited on
Topic archived. No new replies allowed.