C++ DAL - 返回引用或填充传入的引用

发布于 2024-08-06 05:14:15 字数 555 浏览 10 评论 0原文

[编辑 1 - 添加第三个指针语法(感谢 Alex)]

对于 DAL,您更喜欢哪种方法以及为什么:

Car& DAL::loadCar(int id) {}
bool DAL::loadCar(int id, Car& car) {}
Car* DAL::loadCar(int id) {}

如果无法找到汽车,第一个方法返回 null,第二个方法返回 false。

第二种方法将在堆上创建一个 Car 对象并填充从数据库查询的数据。大概(我的 C++ 非常生疏)这意味着代码如下:

Car& DAL::loadCar(int id)
{
    Car *carPtr = new Car();
    Car &car= *carPtr;
    car.setModel(/* value from database */);
    car.setEngineSize(/* value from database */);
    // etc
    return car;
}

谢谢

[EDIT 1 - added third pointer syntax (Thanks Alex)]

Which method would you prefer for a DAL and why out of:

Car& DAL::loadCar(int id) {}
bool DAL::loadCar(int id, Car& car) {}
Car* DAL::loadCar(int id) {}

If unable to find the car first method returns null, second method returns false.

The second method would create a Car object on the heap and populate with data queried from the database. Presumably (my C++ is very rusty) that would mean code along the lines of:

Car& DAL::loadCar(int id)
{
    Car *carPtr = new Car();
    Car &car= *carPtr;
    car.setModel(/* value from database */);
    car.setEngineSize(/* value from database */);
    // etc
    return car;
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角卖回忆 2024-08-13 05:14:15

第二个绝对是更可取的。您正在返回对已新建对象的引用。对于使用该软件的最终用户来说,返回的对象是否需要删除并不明显。另外,如果用户执行类似的操作,

Car myCar = dal.loadCar( id );

指针就会丢失。

因此,第二种方法将内存的控制权交给调用者,并阻止发生任何奇怪的错误。

编辑:通过引用返回是明智的,但仅当父级(即 DAL)类可以控制引用的生命周期时。即,如果 DAL 类中有一个 Car 对象向量,那么返回一个引用将是一件非常明智的事情。

Edit2:我仍然更喜欢第二个设置。第三个比第一个好得多,但最终会让调用者假设对象已初始化。

您还可以提供

Car DAL::loadCar(int id);

并希望接受堆栈副本。

另外不要忘记,您可以创建一种空汽车对象,以便返回一个“有效”的对象,但在所有字段中不会返回任何有用的信息(因此显然被初始化为垃圾数据)。这就是空对象模式。

The second is definitely preferable. You are returning a reference to an object that has been new'd. For an end user using the software it is not obvious that the returned object would require deleting. PLUS if the user does something like this

Car myCar = dal.loadCar( id );

The pointer would get lost.

Your second method therefore puts the control of memory on the caller and stops any weird mistakes from occurring.

Edit: Return by reference is sensible but only when the parent, ie DAL, class has control over the lifetime of the reference. ie if the DAL class had a vector of Car objects in it then returning a reference would be a perfectly sensible thing to do.

Edit2: I'd still prefer the second set up. The 3rd is far better than the first but you end up making the caller assume that the object is initialised.

You could also provide

Car DAL::loadCar(int id);

And hope accept the stack copy.

Also don't forget that you can create a kind of null car object so that you return an object that is "valid"ish but returns you no useful information in all the fields (and thus is obviously initialised to rubbish data). This is the Null Object Pattern.

仅此而已 2024-08-13 05:14:15

既然你无论如何都在堆上分配对象,为什么不考虑 Car * LoadCar() ,如果出现问题,它会返回 NULL。这样,您就没有引用类型的限制(每个引用都必须初始化),并且还可以发出错误情况信号。

Since you are anyways allocating objects on heap, why not to consider Car * LoadCar() which returns NULL if problem occurs. This way you have no restrictions with reference types (each reference must be initialized) and also have means to signal the error case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文