2

I want to know the logic behind how are we getting access to the private member function in Child class.

#include<bits/stdc++.h>
using namespace std;

class Parent{
public:
    virtual void secret(){};
};

class Child:public Parent
{
    string name;
    void secret(){
        cout << "Cant Access this";
    }
public:
    Child(string name):name(name){}
};

int main()
{
    Child c("Whatever");
    Parent* ptr = &c;
    ptr->secret();

    return 0;
}

Output: Cant Access this

Regards,

DK_bhai
  • 337
  • 3
  • 14
  • 4
    **Recommended reading:** [Why should I not #include ?](https://stackoverflow.com/q/31816095/560648) – Asteroids With Wings Aug 04 '20 at 17:05
  • The accepted answer on that duplicate is a masterpiece written by a genius – Asteroids With Wings Aug 04 '20 at 17:06
  • 1
    [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) - Please read that. I wonder why that crap keeps showing up? Is there some websites or *bad* teaching material somewhere promoting that badness? See also [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/5910058) - You are doing two things that are each bad and then combine them into a complete disaster. – Jesper Juhl Aug 04 '20 at 17:07
  • Wow! thank you for the help! I shall read other articles also – DK_bhai Aug 04 '20 at 17:10
  • 1
    The logic is that implementing the rules that it seems you are expecting would mean that the public or private nature of a virtual method would have to be stored and checked at runtime. At present this doesn't happen. I can't see any advantage to requiring this, and I guess the standards writers couldn't either. – john Aug 04 '20 at 17:12
  • Keep in mind that none of these comments will answer your question. I like this question, and the "simple-minded" answer is: Because you are accessing `Parent` semantics, the compiler will allow it. But I will stick around for a really good theoretic answer as to why it's allowed. – Andy Aug 04 '20 at 17:12
  • In `class Parent`, change the method to `private: virtual void secret(){}`, which should clear up the access mystery. Also turn on your compiler warnings, and fix those. – Eljay Aug 04 '20 at 17:13
  • 2
    @Andy "Keep in mind that none of these comments will answer your question" - That's why they are posted as *comments* rather than *answers* – Jesper Juhl Aug 04 '20 at 17:14
  • 1
    Probably comes down to the classic "Not worth the extra compiler complexity required to trap and prevent this." – user4581301 Aug 04 '20 at 17:14
  • 1
    @Andy It's hard to know why it wouldn't be. What result do you expect instead? What kind of error response? – Asteroids With Wings Aug 04 '20 at 17:15
  • @AsteroidsWithWings -- i guess you're right -- What he's doing is slightly unorthodox, which is why it caught me off guard. Got my brain working, that's for sure. – Andy Aug 04 '20 at 17:16
  • @Andy Yeah usually you'd just stick with the one access level :) – Asteroids With Wings Aug 04 '20 at 17:20

0 Answers0