c ++派生纯抽象w /嵌套结构

las*_*bra 5 c++ inheritance properties nested-class pure-virtual

我正在尝试建立一个类似于c#属性概念的语法糖.

我读过这篇文章:原生c ++中的C#属性?.这很有帮助,但缺乏我想要的设计.我还恭敬地不同意那里的几个断言,即属性概念是不好的形式,因为我没有看到一组标题为get_id()和set_id()的方法与暴露相同概念的运算符重载之间的区别,允许代码在使用类时更干净,并且通常不鼓励公共访问私有字段,也将公共实现与私有设计分离.

但是,我提出的代码(受该链接的启发)真的很糟糕,并且很难维护.我想知道是否有人有任何建议来清理它,或者更重要的是,知道更好的方法来做到这一点.

class someClass
{
public:
    struct someProperty_struct{
        virtual someProperty_struct& operator=(const int&){return *this;}
        virtual operator int(){return 0;}
    }someProperty;
};

class derivedClass : someClass
{
    int i;
public:
    struct intProperty: someClass::someProperty_struct
    {
    protected:
        friend class derivedClass;
        derivedClass *outer;
    public:
        virtual someProperty_struct& operator=(const int& value){
            outer->i = value;
            return *this;}
        virtual operator int(){return outer->i;}
    };
    derivedClass()
    {
        intProperty p = intProperty();
        p.outer = this;
        someProperty = p;
    }
};

class consumerClass
{
public:
    someClass* data;
    consumerClass()
    {
        data = (someClass*)(new derivedClass());
    }
    void doSomething()
    {
        data->someProperty = 7;
        int x = data->someProperty;
    }
};
Run Code Online (Sandbox Code Playgroud)

编辑1:我发现我没有透露任何关于我的意图.它将用于调度程序,我们将对类中的所有数据进行大量比较和分配.'someClass'将有效地成为数据的接口(需要的方法很少,大量数据应该相对透明).它将在可执行文件将链接的静态库中定义.'derivedClass'实际上是一个外部实现,在动态加载的dll中实现.这样做的原因是启用dll的"热交换"与另一个实现不同文件后端的dll.我们计划使用插件系统实现xml,sqlite和mysql存储后端来加载它们.

所以基本上,我需要一种设计,允许someClass是一个由derivedClass继承的虚拟接口,它由工厂方法加载,通过插件系统传递,最后由consumerClass使用.

Igo*_*ous 2

所以基本上,我需要一个允许 someClass 成为虚拟接口的设计

如果我理解正确的话,这个意图与你提出的解决方案相矛盾。虚拟继承仅涉及虚拟成员函数。并且您无法动态继承嵌套结构,因为它是 C++ 不支持的词法元编程的主题。

我的建议是仔细考虑是否可以使对象不可变。然后,对构建器/工厂设计模式的一些调整将允许您消除属性的使用。

您也可以考虑编写一个代码生成器。如果您选择了一个好的标记关键字,这可能会很容易。

更新 我添加了一些代码来澄清我的建议。

首先我们准备一些辅助对象。它们应该放在库和客户端都可以访问的头文件中。这些对象永远不会被修改。

GetSetProp <>----> IGetSetProp <----- PropFunctionAdapter

template<class _Ty>
struct __declspec(novtable) IGetSetProp
{
    typedef std::tr1::shared_ptr<IGetSetProp<_Ty>> ptr_t;
    virtual void set_Prop(_Ty const& val);
    virtual _Ty get_Prop() const;
};

template<typename _Ty>
class PropFunctionAdapter : public IGetSetProp<_Ty>
{
    std::function<_Ty(void)> getter;
    std::function<void(_Ty const&)> setter;
public:
    PropFunctionAdapter(std::function<_Ty(void)> _getter, std::function<void(_Ty const&)> _setter)
        : getter(_getter)
        , setter(_setter)
    {
         // One may want to verify that getter and setter are not empty
    }

    virtual ~PropFunctionAdapter() throw() {}

    inline static std::tr1::shared_ptr<typename PropFunctionAdapter<_Ty>> Create(std::function<_Ty(void)> _getter, std::function<void(_Ty const&)> _setter)
    {
        return std::make_shared<typename PropFunctionAdapter<_Ty>>(_getter, _setter);
    }

public:
    void set_Prop(_Ty const& val)
    {
        setter(val);
    }

    _Ty get_Prop() const
    {
        return getter();
    }
};

template<class _Owner, class _Ty>
typename IGetSetProp<_Ty>::ptr_t CreateAdapter(_Owner& source, _Ty(_Owner::*getter)() const, void(_Owner::*setter)(_Ty const&))
{
    return PropFunctionAdapter<_Ty>::Create(
        std::tr1::bind(std::mem_fn(getter), &source),
        std::tr1::bind(std::mem_fn(setter), &source, std::tr1::placeholders::_1));
}

template<class _Ty>
class GetSetProp
{
    typename IGetSetProp<_Ty>::ptr_t prop;
public:
    GetSetProp(typename IGetSetProp<_Ty>::ptr_t _prop)
        : prop(_prop)
    {
    }

    GetSetProp<_Ty>& operator= (_Ty const& val)
    {
        prop->set_Prop(val);
        return *this;
    }

    operator _Ty() const
    {
        return prop->get_Prop();
    }
};
Run Code Online (Sandbox Code Playgroud)

同样,您可以定义 GetProperty 和 SetProperty。

假设您有一个包含两个字段Pressure:int 和Description:string 的数据协定。然后定义数据契约:

class BaseClass
{
public:
    GetSetProp<int> Pressure;
    GetSetProp<std::string> Description;
protected:
    BaseClass(IGetSetProp<int>::ptr_t fieldA, IGetSetProp<std::string>::ptr_t fieldB)
        : Pressure(fieldA)
        , Description(fieldB)
    {
    }

    virtual ~BaseClass() throw() {}
};
Run Code Online (Sandbox Code Playgroud)

及其在库中的实现:

class DerivedClass : public BaseClass
{
public:
    // Here you initialize fields assigning them correspondent setters and getters
    // You may define an ATL-like mapping in order to hide implementation details
    DerivedClass()
        : BaseClass(
            CreateAdapter(*this, &DerivedClass::get_Pressure, &DerivedClass::set_Pressure)
            , CreateAdapter(*this, &DerivedClass::get_Description, &DerivedClass::set_Description))
    {
    }

    virtual ~DerivedClass() throw() {}

private:
    void set_Pressure(int const& value)
    {
        val = value;
    }

    int get_Pressure() const
    {
        return val;
    }

    void set_Description(std::string const& description)
    {
        this->description = description;
    }

    std::string get_Description() const
    {
        return description;
    }

private:
    int val;
    std::string description;
};

// The client-side code
DerivedClass d;
BaseClass& b = d; 
b.Description = "Hello";
std::string descr = b.Description;
b.Pressure = 2;
int value = b.Pressure;
Run Code Online (Sandbox Code Playgroud)

  • 没关系,我读过那个声明。它读起来好像会删除 vtable,但实际上它只是从控制 vtable 的纯抽象类中删除代码,从而实现更快的执行和更小的可执行文件。现在有道理了。每天学习一些东西。 (2认同)