i build these property code:
Code:
template <typename T>
class property
{
private:
T PropertyValue;
std::function<T(void)> getf;
std::function<void(T)> setf;
public:
property(T value)
{
PropertyValue=value;
};
property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)
{
setf=SetFunction;
getf=GetFunction;
}
property& operator=(T value)
{
if(getf==nullptr || setf==nullptr)
PropertyValue=value;
else
setf(value);
return *this;
}
T& operator=(property value)
{
if(value.getf==nullptr || value.setf==nullptr)
return value.PropertyValue;
else
return value.getf();
}
friend ostream& operator<<(ostream& os, const property& dt)
{
if(dt.getf==nullptr && dt.setf==nullptr)
os << dt.PropertyValue;
else if (dt.getf!=nullptr)
os << dt.getf();
return os;
}
friend istream& operator>>(istream &input, property &dt)
{
input >> dt.PropertyValue;
if (dt.setf!=nullptr)
dt.setf(dt.PropertyValue);
return input;
}
friend istream &getline(istream &in, property &dt)
{
getline(in, dt.PropertyValue);
if (dt.setf!=nullptr)
dt.setf(dt.PropertyValue);
return in;
}
};
but i'm getting unexepected results with these test:
Code:
class test
{
public:
int x;
int y;
int getx()
{
return x;
}
void setx(int value)
{
x=value;
}
int gety()
{
return y;
}
void sety(int value)
{
y=value;
}
public:
std::string go()
{
std::ostringstream out;
out << '(' << x << ',' << y << ')' << ':' << x + y << '\n';
return(out.str());
}
test()
{
}
property<int> X=GetProperty<int>(&test::getx, &test::setx,this);
//PROPERTY(int, X,test::getx, test::setx);
PROPERTY(int, Y,test::gety, test::sety);
};
int main()
{
test a;
cout << "what is the x?\n";
cin >> a.X;
cout << "what is the y?\n";
cin >> a.Y;
cout << "\nhere is the result:\n" << a.go() << '\n';
test b = a;
cout << b.x << "\t" << b.y << endl;
cout << "what is a different x?\n";
cin >> b.X;
cout << "what is a different y?\n";
cin >> b.Y;
cout << "\nhere is the result:\n" << b.go() << '\n';
cout << "\noh wait, no; here is the result:\n" << a.go() << '\n';
return 0;
}
i belive is that my class isn't working with class instance\object pointer correctly
but what i'm doing wrong in my class?