Protected member call from parent class should not be allowed

May 2, 2016 at 7:44pm
I have this situation:
1
2
3
4
5
6
7
8
9
10
11
12
class Class1{
public:
  void someFunc(); // This is virtual too in my code, but I don't know if that matters
}

class Class2 : public Class1{
protected:
  using Class1::someFunc; // Hiding it cause I don't want it to be used here
}

Class1* obj = new Class2();
obj->someFunc(); // This works, but it's not supposed to, right? 

Am I missing something? Why the function gets called? Can I prevent this somehow?
May 2, 2016 at 7:57pm
someFunc is a public member of Class1. Line 11 you created a Class1 object. Calling a public member of Class1 is perfectly valid. The fact that it's protected in Class2 is irrelevant.

Had you done the following, you would have gotten an error:
1
2
Class2 * obj2 = new Class2;
obj2->someFunc();   // This should generate an error.  

May 2, 2016 at 8:11pm
@AbstractionAnon So, even if it's virtual it's the same? Ok, my only option then is to overwrite someFunc in my derived class...
Last edited on May 2, 2016 at 8:11pm
Topic archived. No new replies allowed.