封装数据,以便setter是私有的,getter是公共的

时间:2014-05-01 21:10:56

标签: c++ private encapsulation public data-members

我想知道如何在C ++中创建数据实体是最好的,其中" setter"是私人的" getter"是公开的。即实体的创建者应该能够设置数据,但用户/消费者/客户只能获取数据。

让我们考虑实体EntityX:

class EntityX
{
  public:
    EntityX(int x, int y) : x(x), y(y)
    {}
    int GetX() const {return x;}
    int GetY() const {return y;}
  private:
    int x,y; // Effective C++ third edition, Item 22: Declare data members private
}

创建实体并将其返回给客户端的类方法:

const shared_ptr<EntityX> classz::GetEntityX()
{
  shared_ptr<EntityX> entity(new EntityX(1,2));

  return entity;
}

这在我的脑海中使得setter private和getter public,但是如果数据成员是&gt;这个例子是不实际的。 5-10 ...你如何创建一个实体类/结构,使得setter是&#34; private&#34;和#34; getter&#34;是&#34; public&#34;,而不是让构造函数接受所有数据成员变量。

提前致谢

3 个答案:

答案 0 :(得分:2)

如何将 Creator 设置为friendclass EntityX

   class EntityX
    {
      friend class Creator;
      public:
        EntityX(int x, int y) : x(x), y(y)
        {}
        int GetX() const {return x;}
        int GetY() const {return y;}
      private:
        int x,y; // Effective C++ third edition, Item 22: Declare data members private
    };

<强>更新

或者你可以使用模板化的朋友,见下面的代码:

#include <iostream>
#include <memory>

template<class T>
class EntityX
  {
  friend T;
  public:
    EntityX(int x, int y) : x(x), y(y) {}
    int GetX() const {return x;}
    int GetY() const {return y;}
  private:
    int x,y; // Effective C++ third edition, Item 22: Declare data members private
  };

struct Creator
  {
    static const std::shared_ptr<EntityX<Creator>> create() 
      {  
      std::shared_ptr<EntityX<Creator>> entity = std::make_shared<EntityX<Creator>>(1,2);
      entity->x = 1;
      entity->y = 2;
      return entity;
      }
  };

int main()
{
  std::shared_ptr<EntityX<Creator>> const E = Creator::create();
  std::cout << E->GetX() << ", " << E->GetY() << std::endl;

  return 0 ; 
}

答案 1 :(得分:0)

你的getter可以返回一个const&amp;所以...

public:
int const& Getter();
private:
int const& Setter(int value);
带有“setter”和“getter”的

将替换为变量的名称。所以...

public:
int const& X();
private:
int const& X(int value);

您也可以使用此语法编写相同的内容...

const int& X();

只是你想如何写它。

祝你好运,我希望我能提供帮助。

答案 2 :(得分:0)

如下:

struct EntityValues
{
   Type1 value1_;
   Type2 value2_;
   (etc for all the members of Entity
};

class Entity
{
  public:
    Entity () : <set default values> 
    {
    }

    // this avoids the sea-of-parameters problem by bundling the data values
    // into a single parameter.  Data can be added to values by name in random order
    // before it is finally used here.
    Entity(const EntityValues & values) : <set members by pulling data from values>
    {

    }

    // individual public getters.
    Type1 getValue1()const { return value1_;}
    <repeat as necessary for other members>

    // get everything at once 
    // (an alternative to individual getters)
    // 
    void exportValues(EntityValues & values) const
    {
       <copy member to values>
    }

    // optional (this violates the "no public setters" constraint
    // but it does it in a controlled manner.
    void update(const EntityValues & values)
    {
       <set members by pulling data from values>
    }

private:
    <setters (if necessary) and members go here
 };

同样EntityValues可以是在Entity内声明的公共嵌套结构(即struct Entity::Values