Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++; 关于C++中模拟C语言性质的方法的思考,我得到了如下的解决方案: #include <iostream> class obj_with_property { private: class mykey {}; public: class int_property { private: int m_v; public: int_property (int v, mykey) : m_v (v) { } int_property & operator = (int v) { m_v = v; return * this; } operator int () const { return m_v; } }; int_property A; obj_with_property () : A (int_property (0, mykey ())) { } }; int main(int argc, char **argv) { obj_with_property obj; std::cout << obj.A << std::endl; obj.A = 25; std::cout << obj.A << std::endl; return 0; } #包括 类obj_与_属性{ 私人: 类mykey{}; 公众: 类int_属性{ 私人: 国际货币基金组织; 公众: int_属性(int v,mykey):m_v(v){ } int_属性和运算符=(int v){ m_v=v; 归还*这个; } 运算符int()常量{ 返回m_v; } }; int_财产A; 带有属性()的对象:A(int\u属性(0,mykey()){ } }; int main(int argc,字符**argv){ obj_与_属性obj; std::cout_C++_Properties - Fatal编程技术网

C++; 关于C++中模拟C语言性质的方法的思考,我得到了如下的解决方案: #include <iostream> class obj_with_property { private: class mykey {}; public: class int_property { private: int m_v; public: int_property (int v, mykey) : m_v (v) { } int_property & operator = (int v) { m_v = v; return * this; } operator int () const { return m_v; } }; int_property A; obj_with_property () : A (int_property (0, mykey ())) { } }; int main(int argc, char **argv) { obj_with_property obj; std::cout << obj.A << std::endl; obj.A = 25; std::cout << obj.A << std::endl; return 0; } #包括 类obj_与_属性{ 私人: 类mykey{}; 公众: 类int_属性{ 私人: 国际货币基金组织; 公众: int_属性(int v,mykey):m_v(v){ } int_属性和运算符=(int v){ m_v=v; 归还*这个; } 运算符int()常量{ 返回m_v; } }; int_财产A; 带有属性()的对象:A(int\u属性(0,mykey()){ } }; int main(int argc,字符**argv){ obj_与_属性obj; std::cout

C++; 关于C++中模拟C语言性质的方法的思考,我得到了如下的解决方案: #include <iostream> class obj_with_property { private: class mykey {}; public: class int_property { private: int m_v; public: int_property (int v, mykey) : m_v (v) { } int_property & operator = (int v) { m_v = v; return * this; } operator int () const { return m_v; } }; int_property A; obj_with_property () : A (int_property (0, mykey ())) { } }; int main(int argc, char **argv) { obj_with_property obj; std::cout << obj.A << std::endl; obj.A = 25; std::cout << obj.A << std::endl; return 0; } #包括 类obj_与_属性{ 私人: 类mykey{}; 公众: 类int_属性{ 私人: 国际货币基金组织; 公众: int_属性(int v,mykey):m_v(v){ } int_属性和运算符=(int v){ m_v=v; 归还*这个; } 运算符int()常量{ 返回m_v; } }; int_财产A; 带有属性()的对象:A(int\u属性(0,mykey()){ } }; int main(int argc,字符**argv){ obj_与_属性obj; std::cout,c++,properties,C++,Properties,实际上,您可以使用模板轻松地完成此操作-以下是一个基本实现: 现在我在你的评论中看到,你不想要这样的答案,所以这是为了其他人的利益 我自己不会使用这种方法,因为在我创建的大多数库中,我不允许用户直接创建对象——因此,如果没有进一步的包装器,下面的方法将无法工作 template<class T> class property { T value_; public: property(){} property(T v) : value_(v){} pro

实际上,您可以使用模板轻松地完成此操作-以下是一个基本实现: 现在我在你的评论中看到,你不想要这样的答案,所以这是为了其他人的利益

我自己不会使用这种方法,因为在我创建的大多数库中,我不允许用户直接创建对象——因此,如果没有进一步的包装器,下面的方法将无法工作

template<class T>
class property
{
    T value_;
public:
    property(){}
    property(T v) : value_(v){}
    property(property<T> const & other) : value_(other.value_){}

    property<T> & operator=(property<T> const& other)
    {
        value_ = other.value_;
        return *this;
    }

    operator T(){return value_;}
};

class object_with_properties
{
public:
    object_with_properties(){}

    property<int> intP;
    property<double> doubleP;
    property<std::string> strP;
};

现在这段代码有其局限性,因为T必须公开默认构造函数,但它应该适用于允许用户直接创建对象的大多数情况。

实际上,您可以使用模板轻松地完成这项工作-下面是一个基本实现: 现在我在你的评论中看到,你不想要这样的答案,所以这是为了其他人的利益

我自己不会使用这种方法,因为在我创建的大多数库中,我不允许用户直接创建对象——因此,如果没有进一步的包装器,下面的方法将无法工作

template<class T>
class property
{
    T value_;
public:
    property(){}
    property(T v) : value_(v){}
    property(property<T> const & other) : value_(other.value_){}

    property<T> & operator=(property<T> const& other)
    {
        value_ = other.value_;
        return *this;
    }

    operator T(){return value_;}
};

class object_with_properties
{
public:
    object_with_properties(){}

    property<int> intP;
    property<double> doubleP;
    property<std::string> strP;
};

现在这段代码有其局限性,因为T必须公开默认构造函数,但它应该适用于允许用户直接创建对象的大多数情况。

实际上,您可以使用模板轻松地完成这项工作-下面是一个基本实现: 现在我在你的评论中看到,你不想要这样的答案,所以这是为了其他人的利益

我自己不会使用这种方法,因为在我创建的大多数库中,我不允许用户直接创建对象——因此,如果没有进一步的包装器,下面的方法将无法工作

template<class T>
class property
{
    T value_;
public:
    property(){}
    property(T v) : value_(v){}
    property(property<T> const & other) : value_(other.value_){}

    property<T> & operator=(property<T> const& other)
    {
        value_ = other.value_;
        return *this;
    }

    operator T(){return value_;}
};

class object_with_properties
{
public:
    object_with_properties(){}

    property<int> intP;
    property<double> doubleP;
    property<std::string> strP;
};

现在这段代码有其局限性,因为T必须公开默认构造函数,但它应该适用于允许用户直接创建对象的大多数情况。

实际上,您可以使用模板轻松地完成这项工作-下面是一个基本实现: 现在我在你的评论中看到,你不想要这样的答案,所以这是为了其他人的利益

我自己不会使用这种方法,因为在我创建的大多数库中,我不允许用户直接创建对象——因此,如果没有进一步的包装器,下面的方法将无法工作

template<class T>
class property
{
    T value_;
public:
    property(){}
    property(T v) : value_(v){}
    property(property<T> const & other) : value_(other.value_){}

    property<T> & operator=(property<T> const& other)
    {
        value_ = other.value_;
        return *this;
    }

    operator T(){return value_;}
};

class object_with_properties
{
public:
    object_with_properties(){}

    property<int> intP;
    property<double> doubleP;
    property<std::string> strP;
};

现在这段代码有它的局限性,因为T必须公开默认构造函数,但它应该适用于允许用户直接创建对象的大多数情况。

我在支持的默认值和层次结构属性中做了类似的事情,并且是模板化的。我还曾经支持setter(因此,
property=x
将调用一个函数),但由于在我的项目中没有使用它,我放弃了它。我认为您的方法是正确的。问题:“有人知道是否在任何地方讨论过类似的方法吗?”在这种情况下,我不必重新发明轮子。不,我不想讨论如何在C++中模拟属性。我的确切问题是,C++ C++大师中的任何一个是否已经在许多C++书籍或博客中的任何一个地方讨论了这个特定的方法。甚至在维基百科上,但没有引用,不幸的是,我做了这样的事情。在支持的默认值和层次结构属性中,并且是模板化的。我也曾经支持setter(以便
property=x
将调用函数),但由于在我的项目中没有使用它,所以我放弃了它。我认为您的方法是正确的。问题:“有人知道是否在任何地方讨论过类似的方法吗?”在这种情况下,我不必重新发明轮子。不,我不想讨论如何在C++中模拟属性。我的确切问题是,C++ C++大师中的任何一个是否已经在许多C++书籍或博客中的任何一个地方讨论了这个特定的方法。甚至在维基百科上,但没有引用,不幸的是,我做了这样的事情。在支持的默认值和层次结构属性中,并且是模板化的。我也曾经支持setter(以便
property=x
将调用函数),但由于在我的项目中没有使用它,所以我放弃了它。我认为您的方法是正确的。问题:“有人知道是否在任何地方讨论过类似的方法吗?”在这种情况下,我不必重新发明轮子。不,我不想讨论如何在C++中模拟属性。我的确切问题是,C++ C++大师中的任何一个是否已经在许多C++书籍或博客中的任何一个地方讨论了这个特定的方法。甚至在维基百科上,但没有引用,不幸的是,我做了这样的事情。在支持的默认值和层次结构属性中,并且是模板化的。我也曾经支持setter(以便
property=x
将调用函数),但由于在我的项目中没有使用它,所以我放弃了它。我认为您的方法是正确的。问题:“有人知道是否在任何地方讨论过类似的方法吗?”在这种情况下,我不必重新发明轮子。不,我不想讨论如何在C++中模拟属性。我的确切问题是,C++ C++大师中的任何一个都在许多C++书籍或博客中的任何一个地方讨论了这个特定的方法。甚至在维基百科上,但没有引用,不幸的是。