简体   繁体   English

实施模型,结构与类

[英]Implementing models, struct vs. class

Let's say I have a REST service which returns a JSON object with the following scheme: 假设我有一个REST服务,它使用以下方案返回JSON对象:

{
    "id" : 1
    "name" : "Peter"
    "age" : 25
}

I have an application written in C++ which receives this object and deserializes for later use. 我有一个用C ++编写的应用程序,该应用程序接收此对象并反序列化以供以后使用。 I need a data structure to store the object. 我需要一个数据结构来存储对象。

I can implement that using either a struct: 我可以使用任何一种结构来实现:

struct person
{
    int id;
    string name;
    int age;
};

and having a helper function to initialize the struct: 并具有帮助函数来初始化结构:

// Returns a person deserialized from a JSON object
person fromJson(JsonObject obj);

// Usage:
auto personInfo = fromJson(/* JSON object from REST service */);

Or having a class with getters, setters and a constructor which takes a JsonObject as a parameter and does the deserialization by itself: 或者有一个带有getters,setters和构造函数的类,该构造函数将JsonObject作为参数并自行反序列化:

class person
{
public:
    // Deserialized a JSON object and initializes the private fields
    person(JsonObject obj);

    int id() const;
    string name() const;
    int age() const;

    void setId(int id);
    void setName(string name);
    void setAge(int age);

private:
    int _id;
    int _name;
    int _age;
};

// Usage:
person personInfo{ /* JSON object from REST service */ };

Given that the data will be stored on a client machine, displayed, possibly edited then sent back to the REST service, which implementation would be more suitable to use? 假设数据将存储在客户端计算机上,进行显示,可能进行编辑然后再发送回REST服务,那么哪种实现更适合使用? Since I would use the data structure only to store data(using the setters/getters shouldn't trigger anything else than setting/getting the value) I can't really think of any benefits of one over another other than personal preference. 由于我只会使用数据结构来存储数据(使用设置器/获取器除了设置/获取值外,不会触发其他任何事情),除了个人喜好之外,我真的无法想到一个人相对于另一个人的任何好处。

I would go the class route. 我会去上课路线。 In that case the object you're working with can do everything that needs doing (no helper methods to create the object) and it's clear and obvious what's being worked with. 在这种情况下,您正在使用的对象可以完成所有需要做的事情(无需使用辅助方法即可创建该对象),并且正在使用的对象很明显。 What's more, as your application becomes more complicated it'll make it easier to keep the structure and functionality of your objects logical and useful. 而且,随着您的应用程序变得越来越复杂,它将使保持对象的结构和功能的逻辑性和实用性变得更加容易。

In C++, the only difference between a struct and a class is the default access specifier which is public for structs and private for classes. 在C ++中, structclass之间的唯一区别是默认访问说明符,该默认访问说明符对structspublic ,对于类是private的。 You can have a struct with member functions, constructors, move assignment, etc., and it's identical to a class . 您可以使用带有成员函数,构造函数,移动分配等的struct ,该structclass相同。 Doing the de-serialization in the constructor or having a free function is entirely up to you. 在构造函数中进行反序列化或具有自由功能完全取决于您。 Another option would be to have a static member function in person that returns a person object. 另一种选择是有一个static的成员函数person返回一个person对象。

class person {
public:
    static person fromJSON(JsonObject const& src) { ... }
};

auto p = person::fromJSON(source);

In any case, you will need a lot of validation code because you are passing in what could effectively be an arbitrary JSON object, and be sure to follow the rule of 3/5/0 so you don't accidentally inhibit move semantics. 无论如何,您都将需要大量验证代码,因为您传递的可能是有效的任意JSON对象,并且请务必遵循3/5/0的规则,这样就不会意外抑制移动语义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM