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

C++ 如何通过定义并仅提供setter和getter来隐藏字段?,c++,boost,boost-preprocessor,C++,Boost,Boost Preprocessor,我想知道如何隐藏一个不动产字段(不是使其私有或公共,而是强制使用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 compil

我想知道如何隐藏一个不动产字段(不是使其私有或公共,而是强制使用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
...
private:
物业(我的物业);
公众:
财产(单位:百万美元);
...
{
设置_my_a(1);

cout这是维基百科上的链接,而不是重写一个实现示例:


这基本上强制通过getter/setter方法访问属性。要获得所需效果,您需要的升级是将函子传递给这些属性的能力。关于实现这些属性,有很多想法;我不能建议最好的方法,这取决于您的发展需要。就我个人而言,这感觉像是过火了我喜欢使用Pimpl隐藏我的私人细节,只显式提供getter/setter。

与其重写一个实现示例,不如在Wikipedia上链接一个:


这基本上强制通过getter/setter方法访问属性。要获得所需效果,您需要的升级是将函子传递给这些属性的能力。关于实现这些属性,有很多想法;我不能建议最好的方法,这取决于您的发展需要。就我个人而言,这感觉像是过火了我喜欢使用Pimpl来隐藏我的私人细节,只显式地提供getter/setter。

你真的需要使用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; 
  }
};
//property.h
#包括
#定义属性(类型)结构:公共属性
模板
类属性
{
受保护的:
T值;
公众:
虚拟T get(){
返回值;
}
虚空集(T新_值){
值=新的_值;
}
};
用法示例:

// 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());
  }
}
//test.cpp
#包括“property.h”
课堂测试{
公众:
财产(int){}a;
属性(int){
int get(){
返回值*10;
} 
}b;
属性(int){
无效集(int x){
数值=x*200;
} 
}c;
属性(int){
int get(){
返回值*3000;
} 
无效集(int x){
数值=x*443;
} 
}d;
};
main()
{
试验t;
printf(“i\ta\tb\tc\td\t\n”);

对于(inti=0;i您真的需要使用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; 
  }
};
//property.h
#包括
#定义属性(类型)结构:公共属性
模板
类属性
{
受保护的:
T值;
公众:
虚拟T get(){
返回值;
}
虚空集(T新_值){
值=新的_值;
}
};
用法示例:

// 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());
  }
}
//test.cpp
#包括“property.h”
课堂测试{
公众:
财产(int){}a;
属性(int){
int get(){
返回值*10;
} 
}b;
属性(int){
无效集(int x){
数值=x*200;
} 
}c;
属性(int){
int get(){
返回值*3000;
} 
无效集(int x){
数值=x*443;
} 
}d;
};
main()
{
试验t;
printf(“i\ta\tb\tc\td\t\n”);

对于(int i=0;iis这与boost有关?一般的方法是将变量私有化为什么要这样做?如果getter和setter没有自定义,属性就没有意义。@DeadMG:的确-我想自定义geter和setter,但我想知道用geter和setter函数包装字段的一般方法。这与boost?一般的方法是使变量私有为什么要这样做?如果getter和setter没有自定义,属性就没有意义了。@DeadMG:的确-我想自定义geter和setter,但我想知道用geter和setter函数包装字段的一般方法。