c++ - 如何通过定义隐藏字段并仅提供 setter 和 getter ?

标签 c++ boost boost-preprocessor

我想知道如何隐藏一个不动产字段(不是私有(private)或公共(public)而是强制使用setter和getter)并给他提供简单的setter和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 预处理器创建这样的东西?

最佳答案

你真的需要使用boost预处理器吗? 您有一个没有 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("i\ta\tb\tc\td\t\n");
  for (int i=0; i<10; i++) { 
    t.a.set(i);
    t.b.set(i);
    t.c.set(i);
    t.d.set(i);
    printf("%i\t%i\t%i\t%i\t%i\n", i, t.a.get(), t.b.get(), t.c.get(), t.d.get());
  }
}

http://en.wikipedia.org/wiki/Property_(programming)#C.2B.2B 中的维基百科解决方案很好,但需要进行最少的修改才能变得有用,因为如果没有 protected 语句,您将无法编写自己的 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;
}

关于c++ - 如何通过定义隐藏字段并仅提供 setter 和 getter ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11917721/

相关文章:

c++ - 使用 Boost 作为 CMake 的 git 子模块

c++ - Boost.Preprocessor 是否可以实现二维序列?

c++ - 游戏引擎运行时解压

c++ - tcmalloc 巨大的性能差异

c++ - 新标准的特性对 C++11 中的 boost 库实现有重大影响吗?

c++ - Boost property tree xml解析No such node()

boost - 将多个文件预处理为单个文件

c++ - 如何生成用于连接字符串的可变参数宏

c++ - 模板子类覆盖父类的虚函数

c++ - 在另一个使用该字段的方法正在执行时调用修改字段的方法