name-hiding

Implement abstract methods from inherited class

旧巷老猫 提交于 2019-12-10 10:47:49
问题 I am trying to do something I haven't really done before. I basically have 3 classes. Class A is an abstract class with pure virtual methods, Class B is a class on it's own that contains methods with the same name as the virtual methods in Class A. I'm trying to tie everything together in Class C. I'd like to inherit class B and A in C (multiple inheritance), and use the methods from Class B to implement those in class A. This way I create a modular approach. The example below is a very

Overloading a super class's function

自作多情 提交于 2019-12-06 20:59:37
问题 Is there something in the C++ standard that prevents me from overloading a super class's function? Starting with this pair of classes: class A { // super class int x; public: void foo (int y) {x = y;} // original definition }; class B : public A { // derived class int x2; public: void foo (int y, int z) {x2 = y + z;} // overloaded }; I can call B::foo() easily: B b; b.foo (1, 2); // [1] But if I try to call A::foo() ... B b; b.foo (12); // [2] ... I get a compiler error: test.cpp: In function

Implement abstract methods from inherited class

梦想的初衷 提交于 2019-12-06 14:32:41
I am trying to do something I haven't really done before. I basically have 3 classes. Class A is an abstract class with pure virtual methods, Class B is a class on it's own that contains methods with the same name as the virtual methods in Class A. I'm trying to tie everything together in Class C. I'd like to inherit class B and A in C (multiple inheritance), and use the methods from Class B to implement those in class A. This way I create a modular approach. The example below is a very simplified version of my code. class A { virtual int methodA() = 0; virtual int methodB() = 0; virtual int

C++ Nested Scope Accessing

左心房为你撑大大i 提交于 2019-12-06 07:03:22
问题 I recently saw this code in cppreference: string str="global scope"; void main() { string str="main scope"; if (true){ string str="if scope"; cout << str << endl; } cout << str << endl; } Which outputs: if scope main scope This is fine, I understand the whole nested scope thing, and I know that the 'str' inside the if scope will be destroyed when the stack unwinds it at the end of the statement, so it wont be available after that, hence the second print takes the main 'str' as its argument.

Access member field with same name as local variable (or argument)

 ̄綄美尐妖づ 提交于 2019-12-05 12:03:25
Consider following code snippet: struct S { S( const int a ) { this->a = a; // option 1 S::a = a; // option 2 } int a; }; Is option 1 is equivalent to option 2? Are there cases when one form is better than another? Which clause of standard describes these options? option 1 is equivalent to option 2, but option 1 will not work for a static data member EDITED: static data members can be accessed with this pointer. But this->member will not work in static function. but option 2 will work in static function with static member Eg: struct S { static void initialize(int a) { //this->a=a; compilation

Overloading a super class's function

淺唱寂寞╮ 提交于 2019-12-05 02:47:12
Is there something in the C++ standard that prevents me from overloading a super class's function? Starting with this pair of classes: class A { // super class int x; public: void foo (int y) {x = y;} // original definition }; class B : public A { // derived class int x2; public: void foo (int y, int z) {x2 = y + z;} // overloaded }; I can call B::foo() easily: B b; b.foo (1, 2); // [1] But if I try to call A::foo() ... B b; b.foo (12); // [2] ... I get a compiler error: test.cpp: In function 'void bar()': test.cpp:18: error: no matching function for call to 'B::foo(int)' test.cpp:12: note:

Why class member functions shadow free functions with same name?

≡放荡痞女 提交于 2019-11-29 09:51:44
It recently came to my attention that member functions completely shadow free functions with the same name when inside the class. And by completely i mean that every free function with the same name is not considered for overload resolution at all. I can understand why it's done with somwthing like this: void f(); struct S { void f(); void g() { f(); // calls S::f instead of ::f } }; where the functions have identical signatures, its only natural as variable scoping works the same way. But why prohibit unambigious calls where free function has different signature like this: void f(); struct S