函数C ++中标准库函数的重载 [英] Function Overloading for the standard library functions in C++

查看:281
本文介绍了函数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屋!

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