for this solution, i can also write the operator+ like the following:
const plustest operator+(plustest& lhs, plustest& rhs)
{
plustest pt;
pt.n = lhs.n + rhs.n;
return pt;
};
the const plustest operator+(plustest& lhs, plustest& rhs) can also be declared in class as friend as what i comment out.
2.------------------------------------------------------------
#ifndef _PT_CLASS_
#define _PT_CLASS_
class plustest {
public:
plustest(int nn)
{
n = nn;
};
plustest()
{
n = 10;
};
~plustest()
{
n = 0;
};
BTW - pass the arguments to the operator as const references rather than value args - it should be a bit more efficient:
Code:
kota operator+(const kota& lhs, const kota& rhs);
Also there's no point in a const value return from the function. i.e.
Code:
const kota& operator+(const kota&, const kota&)
makes sense, but
Code:
const kota operator+(const kota&, const kota&)
doesn't.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
Bookmarks