ok.. now the code works for a standar properties(like normal variables):
Code:
#include <iostream>
#include <functional>
using namespace std;
template <typename T>
class property
{
private:
T PropertyValue;
std::function<void(void)> getf=NULL;
std::function<void(T a)> setf=NULL;
public:
property()
{
//nothing
};
property(std::function<void(void)> GetFunction,std::function<T(void)> SetFunction)
{
setf=SetFunction;
getf=GetFunction;
}
property& operator=(T value)
{
if(setf==NULL)
PropertyValue=value;
else
setf(value);
return *this;
}
T& operator=(property value)
{
if(getf==NULL)
return PropertyValue;
else
return getf();
}
friend ostream& operator<<(ostream& os, const property& dt)
{
os << dt.PropertyValue;
return os;
}
friend istream& operator>>(istream &input,property &dt)
{
input>>dt.PropertyValue;
return input;
}
};
class test
{
public:
property<string> Name;
property<int> Age;
};
test a;
int main()
{
a.Name="joaquim " "Miguel";
a.Age=10+15;
cout << a.Name << endl;
cout << "what is your name?\n";
cin >> a.Name;
cout << "your name is: " << a.Name << endl;
return 0;
}
my problem is with functions:
Code:
#include <iostream>
#include <functional>
using namespace std;
template <typename T>
class property
{
private:
T PropertyValue;
std::function<void(void)> getf=NULL;
std::function<void(T a)> setf=NULL;
public:
property()
{
//nothing
};
property(std::function<void(void)> GetFunction,std::function<T(void)> SetFunction)
{
setf=SetFunction;
getf=GetFunction;
}
property& operator=(T value)
{
if(setf==NULL)
PropertyValue=value;
else
setf(value);
return *this;
}
T& operator=(property value)
{
if(getf==NULL)
return PropertyValue;
else
return getf();
}
friend ostream& operator<<(ostream& os, const property& dt)
{
os << dt.PropertyValue;
return os;
}
friend istream& operator>>(istream &input,property &dt)
{
input>>dt.PropertyValue;
return input;
}
};
void sethi(string b)
{
b = b + " hi";
}
string gethi()
{
return "hello";
}
class test
{
public:
property<string> Name(&sethi,&gethi);
property<int> Age;
};
test a;
int main()
{
a.Name="joaquim " "Miguel";
a.Age=10+15;
cout << a.Name << endl;
cout << "what is your name?\n";
cin >> a.Name;
cout << "your name is: " << a.Name << endl;
return 0;
}
i get 6 errors and several notes
the 1st 2 errors are:
"error: expected identifier before '&' token"
in these line:
Code:
property<string> Name(&sethi,&gethi);
can anyone advice me where i'm wrong?