+ 4

Crazy observation (c++): "private" does not guarantee protection

Class inheritance can get around private restrictions. Say a parent class A has a public virtual function foo and the same function foo in the child class B is defined under private. When a variable of the child class is created, its private function foo can be directly accessed when the variable is referred to as the parent type. This blew my mind. Example in main: B b; A* a = &b; a->foo(); //accesses the private foo function of b! Edit 2019-11-27: Added example code to avoid difficulty reproducing the subject behavior. Sorry, I should have done this in the first place. https://code.sololearn.com/ccS4GvCOCR6w/?ref=app

22nd Nov 2019, 12:44 PM
James P
16 odpowiedzi
+ 4
Hm, is that only for virtual methods or something? I just tried it with this snippet: class C{ int x=1; }; int main() { C a=C(); C *b=&a; cout << b->x; return 0; } And I got my expected 'that's private, dude' error.
22nd Nov 2019, 12:55 PM
HonFu
HonFu - avatar
+ 3
HonFu it has to do with class inheritance. You need a parent class with a virtual declaration of a function under the public specifier. Then you need to derive a child class with that same function under the private specifier.
23rd Nov 2019, 1:32 PM
James P
+ 3
I want to add that even though C++ lets you do weird stuff like private inheritance, we should still do the sane thing and adhere to Liskov's substitution principle. That is if B is derived from A, and a function requires an A, then you can always substitute a B. Making a public function private in a subclass breaks this rule pretty thoroughly. I think C++ does the right thing given the circumstances. Probably the sanest option would be a compiler error but that's not usually the C++ way of doing things.
24th Nov 2019, 1:33 AM
Schindlabua
Schindlabua - avatar
+ 2
James Parrish What is wrong with this program?? base pointer can access members of derived class as function declared is virtual.. See, you created pointer variable of class A and you are calling the function using that variable..so basically during compile time compiler will interpret it as call to A::foo which is valid, so it will not give error.. During runtime, it will be interpreted as B::foo as base pointer points to child class object..So it will call foo function of class B..This doesn't violate rule of data hiding but it is a feature of c++ i.e Dynamic polymorphism or Run-time polymorphism..
22nd Nov 2019, 1:15 PM
Alaska
Alaska - avatar
+ 2
HonFu Curly braces in declaration of object kid🤔🤔..Change it to () Ps:- I guess it is typing error..Else it will work perfectly..
23rd Nov 2019, 1:50 PM
Alaska
Alaska - avatar
+ 2
AnonymousGuy, uniform initialization {}. Code is copypasted as it was from Playground.
23rd Nov 2019, 1:57 PM
HonFu
HonFu - avatar
+ 2
AnonymousGuy, well that's basically what I did too. 😉
23rd Nov 2019, 2:24 PM
HonFu
HonFu - avatar
+ 1
I have tried to recreate the issue, but somehow it doesn't work yet. Probably I'm doing something wrong? #include <iostream> using namespace std; class Mom { public: virtual void f(); }; class Kid: public Mom { void f() { cout << "Security leak"; } }; int main() { Kid kid = Kid{}; Mom *m = &kid; m->f(); return 0; }
23rd Nov 2019, 1:47 PM
HonFu
HonFu - avatar
+ 1
Ha, so virtual functions need a body! I see I forgot a bit of C++ 😅. Thanks, after adding the body, it (the leak 😉) worked.
23rd Nov 2019, 1:59 PM
HonFu
HonFu - avatar
+ 1
HonFu What is uniform declaration?? I never heard this thing till now..
23rd Nov 2019, 2:02 PM
Alaska
Alaska - avatar
+ 1
Ohh😅 I used this many times but never knew its name..😂😂
23rd Nov 2019, 2:05 PM
Alaska
Alaska - avatar
+ 1
~ swim ~ Yeah..I got it.. thanks☺️
23rd Nov 2019, 2:07 PM
Alaska
Alaska - avatar
+ 1
HonFu I assumed you were trying to create object kid with call to default constructor.. That's why I wrote that..Sorry my mistake😅
23rd Nov 2019, 2:23 PM
Alaska
Alaska - avatar
+ 1
Yeah, swim wrote that up there.
27th Nov 2019, 1:34 PM
HonFu
HonFu - avatar
+ 1
HonFu yeah, I saw that after I posted it so I deleted it.
27th Nov 2019, 2:01 PM
James P