本文介绍了我可以通过专业化这两个类合并吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个类,我写的mock的.NET属性。它似乎做我想要的。但是,不是使用Property1和Property2,我可以写属性,并让它弄清楚我想要的两个类中的哪一个。
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);
}
推荐答案
$ c> Property 保存和操作一个实现,让 Property1
和 Property2
这样的实现,并确定属性
的ctor:
You could make Property
hold and operate on an implementation, let Property1
and Property2
be two such implementations, and determine which of the two to create by Property
's ctor:
template<typename T>
class PropertyInterface {
public:
virtual ~PropertyInterface() {}
virtual T get() const = 0;
virtual void set(const T&) = 0;
};
template<typename T>
class Property1 : public PropertyInterface<T> {
T v;
public:
T get() { return v; }
void set(const T& value) { v = value; }
};
template<typename T>
class Property2 : public PropertyInterface<T> {
Gfn g;
Sfn s;
public:
Property2(Gfn getter, Sfn setter) : g(getter), s(setter) {}
T get() { return g(); }
void set(const T& v) { set(v); }
};
template<typename T>
class Property {
PropertyInterface<T> impl;
public:
Property() : impl(new Property1()) {}
Property(const T& v) : impl(new Property1()) { impl->set(v); }
Property(Gfn getter, Sfn setter) : impl(new Property2(getter, setter)) {}
Property(const Property& p) : impl(new Property1()) { impl->set(p.impl->get()); }
// ...more ctors...
~Property() { delete impl; }
Property& operator=(const Property&) { impl->set(p.impl->get()); return *this; }
Property& operator=(const T& v) { impl->set(v); return *this; }
operator T() const { return impl->get(); }
};
这篇关于我可以通过专业化这两个类合并吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!