我可以通过专业化合并这两个类吗?
这是我编写的用于模拟 .NET 属性的类。它似乎做了我想做的事。但是,我可以编写 Property 并让它找出我想要的两个类中的哪一个,而不是使用 Property1 和 Property2 吗?
#include <cstdio>
template <class T>
class Property{
protected:
Property(const Property&p) {}
Property() {}
public:
virtual Property& operator=(const Property& src)=0;
virtual Property& operator=(const T& src)=0;
virtual operator T() const=0;
};
template <class T>
class Property1 : public Property<T> {
T v;
public:
Property1(const Property1&p) {*this=static_cast<T>(p);}
//Property1() { printf("ctor %X\n", this);}
Property1(){}
Property& operator=(const Property& src) { return*this=static_cast<T>(src); }
Property& operator=(const T& src) {
printf("write %X\n", this);
v = src;
return *this;
}
operator T() const {
printf("Read %X\n", this);
return v;
}
};
template <class T>
class Property2 : public Property<T> {
typedef T(*Gfn)();
typedef void(*Sfn)(T);
Gfn gfn;
Sfn sfn;
public:
Property2(const Property2&p) {*this=static_cast<T>(p);}
//Property2() { printf("ctor %X\n", this);}
Property2(Gfn gfn_, Sfn sfn_):gfn(gfn_), sfn(sfn_) {}
Property& operator=(const Property& src) { return*this=static_cast<T>(src); }
Property& operator=(const T& src) {
printf("write %X\n", this);
sfn(src);
return *this;
}
operator T() const {
printf("Read %X\n", this);
return gfn();
}
};
void set(int v) {}
int get() {return 9;}
Property1<int> a, b;
Property2<int> c(get,set), d(get,set);
void fn(Property<int>& v) { v=v=31; }
int main(){
a=b=5;
c=d=11;
a=c=b=d=15;
fn(a);
fn(c);
}
Here is a class i wrote to mock .NET properties. It appears to do what i want. However instead of using Property1 and Property2 can i write Property and have it figure out which of the two classes i want?
#include <cstdio>
template <class T>
class Property{
protected:
Property(const Property&p) {}
Property() {}
public:
virtual Property& operator=(const Property& src)=0;
virtual Property& operator=(const T& src)=0;
virtual operator T() const=0;
};
template <class T>
class Property1 : public Property<T> {
T v;
public:
Property1(const Property1&p) {*this=static_cast<T>(p);}
//Property1() { printf("ctor %X\n", this);}
Property1(){}
Property& operator=(const Property& src) { return*this=static_cast<T>(src); }
Property& operator=(const T& src) {
printf("write %X\n", this);
v = src;
return *this;
}
operator T() const {
printf("Read %X\n", this);
return v;
}
};
template <class T>
class Property2 : public Property<T> {
typedef T(*Gfn)();
typedef void(*Sfn)(T);
Gfn gfn;
Sfn sfn;
public:
Property2(const Property2&p) {*this=static_cast<T>(p);}
//Property2() { printf("ctor %X\n", this);}
Property2(Gfn gfn_, Sfn sfn_):gfn(gfn_), sfn(sfn_) {}
Property& operator=(const Property& src) { return*this=static_cast<T>(src); }
Property& operator=(const T& src) {
printf("write %X\n", this);
sfn(src);
return *this;
}
operator T() const {
printf("Read %X\n", this);
return gfn();
}
};
void set(int v) {}
int get() {return 9;}
Property1<int> a, b;
Property2<int> c(get,set), d(get,set);
void fn(Property<int>& v) { v=v=31; }
int main(){
a=b=5;
c=d=11;
a=c=b=d=15;
fn(a);
fn(c);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以让
Property
保存并操作一个实现,让Property1
和Property2
成为两个这样的实现,并确定要创建这两个实现中的哪一个属性
的构造函数:You could make
Property
hold and operate on an implementation, letProperty1
andProperty2
be two such implementations, and determine which of the two to create byProperty
's ctor:这不是工厂应该做的事吗?
工厂方法也可以放在属性类中。我更喜欢创建一个额外的工厂类来明确它是工厂模式的使用。
编辑:添加工厂方法的重载
Isn't this what a factory is supposed to do?
The factory method can be put as well in the property class. I prefer to create a extra factory class to make clear that it is a use of the factory pattern.
Edit: Added overload of factory method