C++ Programming ( advantages of by ref & by val) query? / methods of editing struct other than byRef

This is not “advanced programming”; it is the absolute basics of C++.

Whether return-by-value or “out” parameters (implementing using references or pointers) are “best” for any given use case depends on a number of factors, style and opinion being but two of them.

// Return by value
//  T a; a = foo(a);
T foo(const T& in)   // or:   T foo(T in)
{                    //       {
   T out = in;       // 
   out.x = y;        //          in.x = y;
   return out;       //          return in;
}                    //       }

// Out parameter (reference)
//  T a; foo(a);
void bar(T& in)
{
   in.x = y;
}

// Out parameter (pointer)
//  T a; foo(&a);
void baz(T* in)
{
   in->x = y;
}

The question is asking you what the pros and cons are of these three approaches.

Leave a Comment