如何传递一个类对象作为参数 [英] how to pass a class object as parameter
本文介绍了如何传递一个类对象作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有两个代表每个链表的类
so I have two classes representing each a linked list
class list1 {
//some functions
};
class list2 {
//some functions
}
我有我的主要功能,有两个对象LL1和LL2分别用于list1类和list2类。
and I have my main function with two object LL1 and LL2 for respectively list1 class and list2 class.
int main()
{
list1 LL1;
list2 LL2;
}
现在我想调用一个合并这两个列表的函数merge。并将列表的两个对象作为参数。
now I would like to call a function merge that merges those two lists together. and takes the two objects of the lists as parameter.
允许调用函数
void merge(object list1, object list2)
main类似
merge(LL1, LL2);
是可能吗?
推荐答案
是的,可以将对象作为参数传递。
Yes it is possible to pass an object as a parameter.
class list {
//some functions
};
list merge(const list& l1, const list& l2)
{
list mergedList;
//logic to merge l1 and l2 and copy it to mergedList
return mergedList;
}
int main()
{
list LL1;
list LL2;
list mergedList = merge(LL1, LL2);
}
这篇关于如何传递一个类对象作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文