请教高手关于 operator () 操作符?
我想实现一个类似于 C# 的属性,属性自身是没问题的。Int 类自身也没问题,但如果采用模板的方式调用的话会存在问题。我觉得问题的具体细节也许详细描述了,代码中有注明。恳请高手帮忙看看!!!
class Int
{
protected:
int Value;
public:
Int();
Int(const int &value);
Int& operator= (const int &value);
operator int const () const;
};
Int::Int()
{
}
Int::Int( const int &value )
{
Value = value;
}
Int& Int::operator = (const int &value)
{
Value = value;
return *this;
}
Int::operator int const () const
{
return Value;
}
int _tmain(int argc, _TCHAR* argv[])
{
// Int 隐式转换为 int
Int n1 = 1;
int n2 = n1;
property<Int> Width;
Width = 10;
// 此处不支持隐式转换,为什么,该如何处理?
int k = Width;
return 0;
}
// declspec_property.cpp
struct S {
int i;
void putprop(int j) {
i = j;
}
int getprop() {
return i;
}
__declspec(property(get = getprop, put = putprop)) int the_prop;
};
int main() {
S s;
s.the_prop = 5;
return s.the_prop;
}
template <typename DependencyObject>
struct property
{
private:
DependencyObject* Object;
using type = decltype(Object->GetValue());
public:
property (DependencyObject*const object) : Object(object) { }
void operator = (const type& Value)
{
Object->SetValue(Value);
}
operator type const () const
{
return Object->GetValue();
}
};
class Int
{
protected:
int Value;
public:
Int() { }
Int(const int &value) : Value(value) { }
void SetValue (int const& x)
{
Value = x;
}
int GetValue () const
{
return Value;
}
};
int main ()
{
Int a;
property<Int> Width(&a);
Width = 10;
int k = Width;
std::cout << k << std::endl;
}
#include <iostream>
template <typename DependencyObject>
struct property
{
private:
DependencyObject Object;
public:
property() {}
//property() = default; //this is a better choice,1 : easier to read, 2 : able to make your class "trivial"
property (DependencyObject const &object) : Object(object) { }
void operator=(typename DependencyObject::value_type const &Value)
{
Object.SetValue(Value);
}
void operator=(DependencyObject const &Value)
{
Object = Value;
}
operator typename DependencyObject::value_type const () const
{
return Object.GetValue();
}
};
class Int
{
protected:
int Value;
public:
typedef int value_type;//if compiler support, I would use decltype in property class rather using typedef
Int() {}
Int(const int &value) : Value(value) { }
void SetValue (int const& x)
{
Value = x;
}
int GetValue () const
{
return Value;
}
};
int main()
{
property<Int> a(1000);
std::cout << a << std::endl;
a = 10;
std::cout << a << std::endl;
Int b(100);
a = b;
std::cout << a << std::endl;
return 0;
}