-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathinherit_overload_hide.cpp
44 lines (32 loc) · 1.01 KB
/
inherit_overload_hide.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
# Inherit overload hide
Inheritance hides base class methods with the same name even if they have different arguments.
Use `using` to prevent that.
- http://stackoverflow.com/questions/1896830/why-should-i-use-the-using-keyword-to-access-my-base-class-method
- http://stackoverflow.com/questions/4837399/c-rationale-behind-hiding-rule
*/
#include "common.hpp"
int main() {
class Base {
public:
int overload(){ return 1; }
int no_overload(){ return 2; }
int no_base(){ return 3; }
};
class Derived : public Base {
public:
using Base::overload;
int overload(int i) {
return i + 1;
}
int no_base(int i){ return i + 2; }
};
Derived c;
// ERROR: no matching function.
//assert(c.no_base() == 3);
assert(c.no_base(1) == 3);
// Visible because of the using.
assert(c.overload() == 1);
assert(c.overload(1) == 2);
assert(c.no_overload() == 2);
}