//常规中我们有很多办法以减少内存开销,但是每次对象的复制都是一次复制构造函数的执行
  //效率和内存占用居高不下,经难度,本例或可作为一种思路
  //只是本人首次尝试研究,初步构想,实践中是否可行还有待考证
  //敬请广大网友批评指正


#include <iostream>
using namespace std;
class demo
{
public:
demo();
demo(int a);
demo(const demo& tmp);
~demo();
const demo& add(const demo & a, const demo& b);
const demo * constoperator+(const demo & tmp);
void set(int a);
int get()const;
private:
int x;
};
int main()
{
demo fta(5),ftb(6);
const demo * const fts =fta + ftb;
cout<< "ADD:"<< fts->get()<< endl;
return 0;
}
demo::demo()
{
cout<< "Original constructorrunning... ";
cout<< x <<endl;
}
demo::demo(int a)
{
x = a;
cout<< "Set value constructorrunning... ";
}
demo::demo(const demo & tmp)
{
this->x =tmp.x;
cout<< "Copy constructorrunning... ";
cout<< x <<endl;
}
demo::~demo()
{
cout<< "Destructor running... ";
}
const demo & demo::add(const demo& a, const demo & b)
{
this->x =a.x + b.x;
cout<< "Two objects... ";
return *this;
}
const demo * const demo::operator+(const demo& tmp)
{
cout<< "Direct memory address accessmode, no objects will be copied. ";
this->x =this->x + tmp.x;
return this;
}
void demo::set(int a)
{
x = a;
}
int demo::get()const
{
return x;
}