C++中using的四大用法总结
请直接看:C++中using的四大用法总结
补充说明:第四点就是为了防止如下错误:
#include <iostream>
using namespace std;
class base
{
public:
void test()
{
cout << "base::test()" << endl;
}
void test(double)
{
cout << "base::test(double)" << endl;
}
void test(int)
{
cout << "base::test(int)" << endl;
}
};
class derived : public base
{
public:
void test()
{
cout << "derived::test()" << endl;
}
// using base::test;
};
int main(int argc, char **argv)
{
derived *pb = new derived();
pb->test(2.4);
return 0;
}
上述代码会提示:没有函数derived::test(double)
正确代码:
#include <iostream>
using namespace std;
class base
{
public:
void test()
{
cout << "base::test()" << endl;
}
void test(double)
{
cout << "base::test(double)" << endl;
}
void test(int)
{
cout << "base::test(int)" << endl;
}
};
class derived : public base
{
public:
using base::test;
void test()
{
cout << "derived::test()" << endl;
}
// using base::test;
};
int main(int argc, char **argv)
{
derived *pb = new derived();
pb->test(2.4);
return 0;
}