[英]how to pass an object to class as a parameter
If i have several similar classes, and i have to perform the same set of functions accessing each class, So i thought of making a generic function which takes the class and pointer as parameter. 如果我有几个相似的类,并且我必须执行访问每个类的相同功能集,那么我考虑制作一个通用函数,该函数将类和指针作为参数。 how to do that?
怎么做?
for example: 例如:
CClass1 * pClass1 = "something";
CClass2 * pClass2 = "something";
CClass3 * pClass3 = "something";
pClass1, pClass2 and pClass3 are being used in a similar way, so i need to make a generic function passing pClass1,2,3 as parameters instead of repeating the function for three pointers.. so is ther any way in which u can send the class as well as object as a parameter?? pClass1,pClass2和pClass3的使用方式相似,因此我需要制作一个以pClass1,2,3作为参数的泛型函数,而不是对三个指针重复该函数。因此,您可以通过任何方式发送类以及对象作为参数?
I am not sure if I get your question right: The normal way in C++ to have common function for classes is to derive all classes from the very same basis class. 我不确定是否能正确回答您的问题:C ++中具有类通用功能的通常方法是从完全相同的基类派生所有类。
Beside of that, you can overload functions and operators. 除此之外,您还可以重载函数和运算符。 Then, you can apply the same (by name) function but call the function for the proper type.
然后,您可以应用相同的(按名称)函数,但为正确的类型调用该函数。
While your example code does not make sense, it seems like you are describing something which can be done using templates: 虽然示例代码没有意义,但似乎您正在描述可以使用模板完成的操作:
template<class T>
void do_something() {
T t = "something";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.