static and base classes

R

rodchar

hey all,
what are some different ways around the following violation:

i can't access a variable inside the base class from inside a static method
of the derived class

thanks,
rodchar
 
K

Ken Foskey

hey all,
what are some different ways around the following violation:

i can't access a variable inside the base class from inside a static
method of the derived class

Could you show a small code sample of what you are trying to do.

Unless the variable inside the base class is static as well this makes no
sense. If it is static then it must be protected, internal or public to
allow access.

Ken
 
B

Ben Voigt [C++ MVP]

Ken Foskey said:
Could you show a small code sample of what you are trying to do.

Unless the variable inside the base class is static as well this makes no
sense. If it is static then it must be protected, internal or public to
allow access.

I'd guess it is:

class Base { protected int i; }
class Derived { public static int Inspect(Base b) { return b.i; } }

No, that won't work because it breaks encapsulation (and security).
Protected access only grants you access through a reference known to be your
(derived) type. That could be a variable of type Derived or any further
derived type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top