封装数据,使setter是私有的,getter是公共的
Encapsulating data so the setter is private and getter is public
我想知道如何最好地在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是私有的,getter是公共的,但如果数据成员>5-10,这个例子就不实用了……如果不让构造函数接受所有数据成员变量,你如何创建一个实体类/结构,使setter是"私有的","getter"是"公共的"。
提前感谢
将创建者设置为friend
到class 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 ;
}
您的getter可以返回一个const&所以…
public:
int const& Getter();
private:
int const& Setter(int value);
用"setter"answers"getter"替换为变量的名称。所以…
public:
int const& X();
private:
int const& X(int value);
你也可以用这个语法写同样的东西。。。
const int& X();
只是你想怎么写的问题。
祝你好运,我希望我能帮上忙。
像这样的东西怎么样
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
(内部声明的公共嵌套结构
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 如何通过 getter 函数删除矢量的元素?
- 如果构造函数和析构函数可以获取和显示(打印)数据,为什么我们需要 getter 和 setter?
- Getter 函数,用于在 2d 数组是类的数据成员时检索 2d 数组元素
- C++数据将getter绑定到XAML中的一个字段,将setter绑定到AML中的另一个字段
- setter /getter和分层数据结构
- 封装数据,使setter是私有的,getter是公共的
- 使用const getter风格的函数代替静态数据成员