c++ - 使用结构作为函数参数的函数重载
问题描述
我可以使用结构作为参数的函数重载吗?如果是,我该如何实施?这种方式行得通吗?
void function1(struct_a *a, struct_b *b, int i)
{
cout << "abc";
}
void function1(struct_c *c, struct_d *d)
{
cout << "def";
}
int main()
{
struct_a *a = (struct_a *)<some-data-a>;
struct_b *b = (struct_b *)<some-data-b>;
struct_c *c = (struct_c *)<some-data-c>;
struct_d *d = (struct_d *)<some-data-d>;
function1(a, b, 1);
return 0;
}
解决方案
是的你可以。您可以重载任何函数,具体取决于:1)参数数量 2)参数的数据类型
结构是用户定义的数据类型,因此您可以通过各种方式使用它们来区分不同的重载函数并将它们与原始数据类型混合。
在您的情况下,您可以通过以下方式调用第一个函数(带有 3 个参数):
function1(a, b, 1);
对于使用第二个函数(带有 2 个参数),您只需提供 struct_c 和 struct_d 的 2 个参数:
function1(c, d);
推荐阅读
- amazon-web-services - 从 .ebextentions 文件夹复制文件
- typescript - 我可以禁用全局类型声明吗?
- javascript - 在按键点击时选择下拉触发器
- java - 使用 ArrayList 上方法的返回值
- c# - C# Delegate 函数总是为一半的数据集解析为真
- mysql - 在域表具有自由文本的“其他”选项的情况下,数据库规范化的最佳实践是什么?
- python - Open3d Python,使用 TriangleMesh.create_from_point_cloud_alpha_shape() 键入错误
- git - 是否可以 git rebase 并跳过所有冲突?
- c# - 处理 .net 核心中的电源事件
- python - 使用列表推导对字符串列表进行子串化