Monday, February 16, 2009

Base Class access specifiers : C++

Access is controlled, not visibility in C++. It’s easy with a easy example to wrap your head around this.

Rule 1) Whether the derived class declares the base class using the public access specifier in the class-head

Private base class members: Inaccessible

Protected: Will be protected unless its private

Public: Will be public unless private or protected is used.

Rule 2) What the access to the member is in the base class.

A derived class without a base-class access specifier, is considered private if the derived class declaration uses the class keyword. The derivation is considered public if the derived class declaration uses the struct keyword.

class Derived : Base

is similar to, class Derived : private Base

struct Derived : Base

is similar to, struct Derived : public Base

MSDN example illustrates this very clearly.

del.icio.us Tags:

Technorati Tags:

No comments:

Post a Comment