简体   繁体   English

具有setter和getter的C ++属性

[英]C++ Properties with setter and getter

Hi im new to C++ for arduino and i know properties from many other languages. 嗨,我是arduino的C ++的新人,我知道许多其他语言的属性。

In C# I can write Properties with setter and getter like this: 在C#中,我可以像这样用setter和getter编写Properties:

public int foo
{
  get
  {
    return bar;
  }
  set
  {
    bar = value;
  }
}

Is something like this possible in C++? 在C ++中是否可能发生这种情况? I cant find a fancy solution which i can split into header and source files. 我找不到可以将其拆分为头文件和源文件的理想解决方案。 :/ :/

C++ doesnt have C#-style-like properties. C ++没有类似C#样式的属性。

but you can have a setter and getter function declared in header and then define it in source files. 但是您可以在标头中声明一个setter和getter函数,然后在源文件中对其进行定义。

example: 例:

///in header file
class Test
{
    public:
         int getX();
         void setX(int newX);
    private:
         int x;
}

//in source file

int Test::getX()
{
     return x;
}

void Test::setX(int newX)
{
      x = newX;
}

EDIT: 编辑:

when working with header files you should put a header guards on it. 使用头文件时,应在其上放置头防护。

read about this: https://en.wikipedia.org/wiki/Include_guard 请阅读以下内容: https : //en.wikipedia.org/wiki/Include_guard

class Foo
{
public:
    int get()
    {
        return bar;
    }

    void set( int new_bar )
    {
        bar = new_bar;
    }

private:
    int bar;
};

This will give you a class name Foo. 这将为您提供一个类名Foo。 This class has a private member, bar. 该课程有一个私人会员,bar。 You can set the value of bar through the set() method, and you can get the value of bar via the set() method. 您可以通过set()方法设置bar的值,并且可以通过set()方法获取bar的值。

Like C# it is not possible same way but you can try some thing like below. 像C#一样,这是不可能的,但是您可以尝试以下类似的方法。

class Foo
{
public:
    void bar(int val){ _bar=val;}
    int bar(){ return  _bar;}

private:
    int _bar;
};

Writing wrapping template class will help you. 编写包装模板类将对您有所帮助。 In the class, by overloading some special functions, you can chase access to underlying object. 在类中,通过重载一些特殊功能,您可以追逐对基础对象的访问。

template <class T, class Object, T(Object::*real_getter)(), T(Object::*real_setter)(T const &)> 
class RWProperty 
{ 
private:
    Object * my_object;
public:
    RWProperty() : my_object(0) {}
    RWProperty(Object * me = 0) : my_object(me) {}
    void operator()(Object * obj) { my_object = obj; }
    T operator()() const { return (my_object->*real_getter)(); }
    T operator()(T const & value) { return (my_object->*real_setter)(value); }
    T get() const { return (my_object->*real_getter)(); }
    T set(T const & value) { return (my_object->*real_setter)(value); }
    operator T() const { return (my_object->*real_getter)(); }
    T operator=(T const & value) { return (my_object->*real_setter)(value); }
    typedef T value_type;
};

//Use case from here.
#include <iostream>

struct Widget
{
private:
    int value_;
    int get(void) { std::cout << "Real getter called." << std::endl; return value_; }
    int set(const int& _value) {  std::cout << "Real setter called." << std::endl; return value_ = _value; }
public:
    RWProperty<int, Widget, &get, &set> value;
    Widget(void):value(this){}
};


int main(void)
{
    Widget w;
    w.value = 31337; //Call Widget::set(Called real setter in RWProperty) indirectly.
    std::cout << w.value; //Call Widget::get(Called real getter in RWProperty)
}

You should also read "C++ Properties - a Library Solution" for another information. 您还应该阅读“ C ++属性-库解决方案”以获取其他信息。 In the paper, several type of property classes, typed by its access authority, are introduced. 在本文中,介绍了几种类型的属性类,按其访问权限分类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM