小编use*_*739的帖子

C++:为什么成员函数优先于全局函数

为什么在下一个程序中成员函数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)

c++ overloading

8
推荐指数
2
解决办法
735
查看次数

C++:从模板参数继承

在下一个代码示例中:

#include <iostream>
using namespace std;
int f() {
    return 0;
}
struct A {
    int f()    {
        return 1; }
};
template<class T>
struct C : public T {
    C() {
        cout << f() << endl;
    } };
int main() {
    C<A> c; // prints 0
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我将继承更改为非模板,如下所示: struct C : public A

然后它打印"1"而不是0.

这是为什么?

c++ inheritance templates struct

3
推荐指数
1
解决办法
132
查看次数

标签 统计

c++ ×2

inheritance ×1

overloading ×1

struct ×1

templates ×1