函数C ++中标准库函数的重载 [英] Function Overloading for the standard library functions in C++
问题描述
我有一个自由功能作为类的一部分。在类的构造函数中,我做一些 malloc
操作。所以在 destructor
我试图释放内存。但VS10编译器抱怨
free(pointer);
与我类的自由函数的签名不匹配。
所以问题是在一个类中,如果我们实现了与标准库函数具有相同名称的方法。如何调用一个在另一个。
请注意,
:
void YourClass :: free(args){
:: free(your_member);
}
这将取得免费
函数在全局命名空间中,而不在你的类中。
#include< cstdio>
到
std
命名空间中,因此
和
和 malloc
c $ c> std :: free std :: malloc
也可以工作。
<还应该考虑使用
new
/ delete
,以及智能指针。) I have a free function as part of a class. In the constructor for the class i am doing some malloc
operations . So in the destructor
i am trying to free that memory. But VS10 compiler complains that the
free(pointer);
doesn't match the signature of the free function of my class.
So question is In a class wherein if we have implemented methods which have same names as that of standard library functions . How to call one over the other.
Regards,
You should qualify your call to the function:
void YourClass::free(args) {
::free(your_member);
}
This will pick up the free
function in the global namespace and not in your class.
#include <cstdio>
also puts free
and malloc
into the std
namespace, so std::free
and std::malloc
will also work.
(Use of new
/delete
should also be considered, as well as smart pointers.)
这篇关于函数C ++中标准库函数的重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!