Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Could you explain 'type conversions (casts)' in class access control to me?

22 views
Skip to first unread message

fl

unread,
Jun 1, 2015, 6:26:41 PM6/1/15
to
Hi,

When I read access control to class member on web of MSDN, I am confused by
its last sentence:

'This protection is lost when explicit type conversions
(casts) are performed.'

Please see the pasted below. I do not know there is a cast in access control
yet. Could you explain it to me?


Thanks,




You can increase the integrity of software built with C++ by helping control
access to class member data and functions. Class members can be declared as
having private, protected, or public access, as shown in the following table:

......

Access control helps prevent you from using objects in ways they were not
intended to be used. This protection is lost when explicit type conversions
(casts) are performed.
Message has been deleted

fl

unread,
Jun 1, 2015, 7:52:20 PM6/1/15
to
After I post my question, I guess that the original statement means the
specific access word before class derivation. It can change the access
level in the derived class. This can be called a cast?

class Foo
{
public:
Foo() { std::cout << "Foo's constructor" << std::endl; }
};
class Bar : public Foo
or
class Bar : protected Foo
or
class Bar : private Foo
{
public:
Bar() { std::cout << "Bar's constructor" << std::endl; }
};

Paavo Helde

unread,
Jun 2, 2015, 1:35:51 AM6/2/15
to
fl <rxj...@gmail.com> wrote in
news:9a550a88-3242-4b99...@googlegroups.com:
Maybe they think something like:

class Bar {
private:
int i;
};

Bar a;
* (int*) &a = 42;

or

class Foo {
public:
int i;
};

class Bar: private Foo {};

Bar a;
((Foo*) &a)->i = 42;

If so, then the casts are not only violating the intended access rules,
but are also pretty fragile and unportable (as they assume a certain
memory layout).
0 new messages