c++ - Is it safe to access Base protected member this way? -


i have 2 classes:

class base { protected:    int x; };  class der: public base {    void acc(base& b)    {       b.*(&der::x) = 5;    } }; 

is safe access base protected member way (&der::x) ? i'm worried point wrong variable.


this way pass accessing protected member of base class object.

enter image description here

the above code:

enter image description here

for might find difficult understand below line

b.*(&der::x) = 5; 

it can written as:

b.*(&(der::x)) = 5; 

as scope resolution operator can omitted have 1 variable named x in base , derived class.

b.*(&x) = 5; 

which nothing taking address of x , again dereferencing it. can written as:

b.x = 5; 

which hope might aware of. code won't compile if use b.x instead of 'b.*(&der::x) = 5;' 'x' protected , used bypass compiler rule prevents writing b.x = 5;


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -