A function may return an object to the caller : return object « Function « C++ Tutorial






#include <iostream>
using namespace std;
   
class myclass {
  int i;
public:
  void set_i(int n) { i=n; }
  int get_i() { return i; }
};
   
myclass f();  // return object of type myclass
   
int main()
{
  myclass o;
   
  o = f();
   
  cout << o.get_i() << "\n";
   
  return 0;
}
   
myclass f()
{
  myclass x;
   
  x.set_i(1);
  return x;
}








7.9.return object
7.9.1.Return objects from a function
7.9.2.A function may return an object to the caller