#ifndef WEIGHT_H
#define WEIGHT_H
#include <memory>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//接口
class IMethed
{
public:
virtual void setname(std::string name) = 0;
virtual void setnumber(double value) = 0;
virtual double getnumber(int index) = 0;
virtual int getcount() = 0;
};
//接口实现类,含impl技术应用
class Weight : public IMethed
{
public:
Weight();
~Weight();
Weight(Weight&& rhs);// = default;
Weight& operator=(Weight&& rhs);// = default;
void setname(std::string name);
void setnumber(double value);
double getnumber(int index);
int getcount();
private:
struct Impl;
std::unique_ptr<Impl> m_impl;
};
//代理类
class Proxy: public IMethed
{
Weight impGG;
public:
Proxy(Weight&&);
void setname(std::string name);
void setnumber(double value);
double getnumber(int index);
int getcount();
};
#endif // WEIGHT_H
#include "weightproxy.h"
struct Weight::Impl {
std::string name;
vector<double> data;
};
Weight::Weight()
: m_impl(new Impl())
{
}
Weight::~Weight()
{
}
Weight& Weight::operator=(Weight&& rhs)
{
m_impl = std::move(rhs.m_impl);
return *this;
}
Weight::Weight(Weight&& rhs)
{
m_impl = std::move(rhs.m_impl);
}
void Weight::setname(std::string name)
{
m_impl->name = name;
}
void Weight::setnumber(double value)
{
m_impl->data.push_back(value);
}
double Weight::getnumber(int index)
{
return m_impl->data[index];
}
int Weight::getcount()
{
return m_impl->data.size();
}
Proxy::Proxy(Weight&& w)
{
impGG = std::forward<Weight>(w);
}
void Proxy::setname(std::string name)
{
impGG.setname(name);
}
void Proxy::setnumber(double value)
{
impGG.setnumber(value);
}
double Proxy::getnumber(int index)
{
return impGG.getnumber(index);
}
int Proxy::getcount()
{
return impGG.getcount();
}
int main () {
Weight w;
Weight c;
c.setname("li li");
c.setnumber(10.0);
c.setnumber(11.0);
c.setnumber(12.0);
c.setnumber(13.0);
for(int i=0;i<c.getcount();i++)
cout << "c :"<< c.getnumber(i) <<endl;
w = std::move(c);
for(int i=0;i<w.getcount();i++)
cout << "w :"<< w.getnumber(i) <<endl;
Proxy p(std::move(w));
for(int i=0;i<p.getcount();i++)
cout << "p :"<< p.getnumber(i) <<endl;
for(int j=0;j<w.getcount();j++)
cout << "ww :"<< w.getnumber(j) <<endl;
return 0;
}