考虑一下代码:
#include <stdio.h>
class Base {
public:
virtual void gogo(int a){
printf(" Base :: gogo (int) \n");
};
virtual void gogo(int* a){
printf(" Base :: gogo (int*) \n");
};
};
class Derived : public Base{
public:
virtual void gogo(int* a){
printf(" Derived :: gogo (int*) \n");
};
};
int main(){
Derived obj;
obj.gogo(7);
}
Run Code Online (Sandbox Code Playgroud)
得到此错误:
>g++ -pedantic -Os test.cpp -o test test.cpp: In function `int main()': test.cpp:31: error: no matching function for call to `Derived::gogo(int)' test.cpp:21: note: candidates are: virtual …
启用swapSTL算法的正确方法是什么?
1)会员swap.是否std::swap使用SFINAE技巧来使用该成员swap.
2)自由站立swap在同一名称空间中.
3)部分专业化std::swap.
4)以上所有.
谢谢.
编辑:看起来我没有清楚地说出我的问题.基本上,我有一个模板类,我需要STL algos来使用我为该类编写的(高效)交换方法.
为什么在下一个程序中成员函数foo优先于全局foo,尽管全局foo匹配类型?
#include <iostream>
using namespace std;
void foo(double val) { cout << "double\n";}
class obj {
public:
void callFoo() { foo(6.4); }
private:
void foo(int val) {cout << "class member foo\n"; }
};
int main() {
obj o;
o.callFoo();
}
Run Code Online (Sandbox Code Playgroud)