Protected member call from parent class should not be allowed

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?
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.  

@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
Topic archived. No new replies allowed.