简体   繁体   中英

C++ Properties with setter and getter

Hi im new to C++ for arduino and i know properties from many other languages.

In C# I can write Properties with setter and getter like this:

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

Is something like this possible in C++? I cant find a fancy solution which i can split into header and source files. :/

C++ doesnt have C#-style-like properties.

but you can have a setter and getter function declared in header and then define it in source files.

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

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. This class has a private member, bar. You can set the value of bar through the set() method, and you can get the value of bar via the set() method.

Like C# it is not possible same way but you can try some thing like below.

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. In the paper, several type of property classes, typed by its access authority, are introduced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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