如何通过定义隐藏字段,仅提供二传手和getter



我想知道如何隐藏一个不动产字段(不是将其设为私有或公共,而是强制使用二传手和getter),并为他提供简单的二传手和getter。所以我想知道如何创建这样的 api:

private:
    Property( int my_a);
public:
    Property( int my_b);
...
{
 set_my_a(1);
 cout << get_my_a() << endl;
 // my_a = 13; // will cause compiler error
...

如何通过 Boost 预处理器创建这样的东西?

你真的需要使用升压预处理器吗?您有一个没有提升的解决方案如下:

// property.h    
#include <stdio.h>
#define property(type) struct : public Property <type>  
template <typename T>
class Property 
{
protected:
  T value;
public:
  virtual T get() { 
    return value; 
  }
  virtual void set(T new_value) { 
    value = new_value; 
  }
};

使用示例:

// test.cpp
#include "property.h"
class Test {
public:
  property(int) {} a;
  property(int)  {
    int get() { 
      return value * 10; 
    } 
  } b;
  property(int) {
    void set(int x) {
      value = x * 200;
    } 
  } c;
  property(int) {
    int get() { 
      return value * 3000; 
    } 
    void set(int x) {
      value = x * 443;
    } 
  } d;
};
main()
{
  Test t;
  printf("itatbtctdtn");
  for (int i=0; i<10; i++) { 
    t.a.set(i);
    t.b.set(i);
    t.c.set(i);
    t.d.set(i);
    printf("%it%it%it%it%in", i, t.a.get(), t.b.get(), t.c.get(), t.d.get());
  }
}

http://en.wikipedia.org/wiki/Property_(programming)#C.2B.2B中的维基百科解决方案很好,但需要最少的修改才能变得有用,因为没有受保护的语句,你就无法编写自己的getter和setter。

#include <iostream>
template <typename T> 
class property {
protected:
  T value;
public:
  T & operator = (const T &i) {
    ::std::cout << i << ::std::endl;
    return value = i;
  }
  operator T const & () const {
    return value;
  }
};
struct Bar {
  property <bool> alpha;
  struct :public property <int> {
    int & operator = (const int &i) {
      ::std::cout << "new setter " << i << ::std::endl;
      return value = i;
    }
  } bravo;
};
main() 
{
  Bar b;
  b.alpha = false;
  b.bravo = (unsigned int) 1;
}

如果需要,您可以再更改一点:

#include <iostream>
#define SETTER(type) public: type& operator=(const type new_value)
#define GETTER(type) public: operator type const & () const
template <typename T> 
class Property {
protected:
  T value;
public:
  T & operator = (const T &i) {
    ::std::cout << i << ::std::endl;
    return value = i;
  }
  template <typename T2> T2 & operator = (const T2 &i) {
    ::std::cout << "T2: " << i << ::std::endl;
    T2 &guard = value;
    throw guard; // Never reached.
  }
  operator T const & () const {
    return value;
  }
};
struct Bar {
  Property <bool> alpha;
  struct:Property <int> {
    SETTER(int) {
      value = new_value * 1000;
      ::std::cout << "new method " << new_value << ::std::endl;
      return value;
    }
    GETTER(int) {
      return value/1000;
    }
  } bravo;
};
main() 
{
  Bar b;
  b.alpha = false;
  b.bravo = (unsigned int) 1;
  ::std::cout << b.bravo << ::std::endl;
}

与其重写一个实现示例,不如说这是维基百科上一个链接:http://en.wikipedia.org/wiki/Property_(programming)#C.2B.2B

这基本上强制通过 getter/setter 方法访问属性。获得所需效果所需的升级是能够将函子传递给这些属性。关于实施这些有很多想法;我不能建议的最佳方法,取决于您的发展需求。就个人而言,感觉就像过度工程化,更喜欢只使用 Pimpl 来隐藏我的私人详细信息,并明确地提供 getter/setter。

最新更新