使用函数的结构参数而不是多个参数的C ++? [英] C++ using struct arguments for functions instead of multiple arguments?

查看:37
本文介绍了使用函数的结构参数而不是多个参数的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人认为使用类或结构传递参数有好处吗?

Anybody think there are advantages to using a class or struct to pass arguments ?

像而不是

f(int,float,string)

f(Args)

其中 Args 是具有 int float string 成员的 struct .

Where Args is struct with int,float,string members.

Advantage易于创建多个默认参数,并且在添加新参数时无需更改函数签名.

Advantage is easy to create multiple default parameters and not have to change function signature when new arguments added.

推荐答案

显而易见的好处是对语义相关的数据项进行逻辑分组.

The obvious benefit would be to have logical grouping of semantically related data items.

完成后,在结构上添加一些(成员)操作,以确保您的不变性.

Once you do, add some (member) operations on the structure that will guarantee your invariants.

封装提高了代码的抽象级别,这使维护/推理变得更加容易.

The encapsulation raises the abstraction level of your code and this makes it easier to maintain/reason about.

另请参见迪米特法律

这篇关于使用函数的结构参数而不是多个参数的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆