从C ++ Class成员函数访问C标准库的函数 [英] Access C standard library's function from C++ Class member function

查看:83
本文介绍了从C ++ Class成员函数访问C标准库的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// Header File

class CPrint
{ 
  void printf()
};

// Source File

void CPrint::printf()
{
  printf("Test"); // Here i want to access printf function of C library
  //But i gives the error no matching function for CPrint::printf(char[4])
}

推荐答案

你必须告诉编译器printf()函数不是你的类,而是来自全局名称空间:

You must tell the compiler that the printf() function is not the one of your class but from global name space:
void CPrint::printf()
{
  ::printf("Test");
}



一般来说,使用标准库函数的名称作为成员函数名称是个坏主意。对于C ++类,通常让成员函数名称以大写字母开头:


In general it is a bad idea to use names of standard library functions as member function names. With C++ classes, it is common to let member function names begin with an uppercase letter:

class CPrint
{
  void Printf();
};

// Source File

void CPrint::Printf()
{
  printf("Test");
}


这篇关于从C ++ Class成员函数访问C标准库的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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