Method A:
Object& GetStuff()
{
return obj;
}
Method B:
void GetStuff(Object& obj)
{
obj = otherobj;
}
When should I use each of them?Thanks
Jack
Posted 07 September 2012 - 11:13 PM
Posted 08 September 2012 - 06:47 AM
class vector { ... };
vector operator+(vector const& lhs, vector const& rhs)
{
...
}
int main()
{
vector a(1, 1), b(2, 2), c(3, 3);
vector d = a + b + c;
}
See how returning the value from the function allows the functions to be chained using a trditional algebraic syntax?
Posted 08 September 2012 - 08:48 AM
Edited by BinaryPhysics, 08 September 2012 - 08:49 AM.
Posted 08 September 2012 - 09:23 AM
Posted 09 September 2012 - 12:18 PM
Object& GetStuff()Lets you operate on the returned variable.
Object &operator<<(parameter)
{
//use the parameter here.
return *this;
}
Object myObject;
myObject << parameter << parameter << parameter;
(((myObject << parameter) << parameter) << parameter);int GetStuff(Object& obj)
int x, y; void FillXY(x, y);
std::ifstream file("file.txt");
int maxChars = 50;
char *buffer = new char[maxChars];
int actualNumberOfCharsRead [out] = file.readsome(buffer [in and out], maxChars [in]);Object GetStuff();
std::vector<std::string> GetStuff(std::vector<std::string> passByValue) {return passByValue;}
std::vector<std::string> veryLargeVector;
veryLargeVector.resize(2000, "very large string containing alot of data"); //2000 copies of the string "very large string containing alot of data".
std::vector<std::string> notACopy = GetStuff(veryLargeVector); //No copy made - not in passing the variable in, and not in return the variable.Edited by Servant of the Lord, 01 November 2012 - 09:22 PM.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal