Topic: Access qualifier redeclaration
Author: bs@research.att.com (Bjarne Stroustrup)
Date: 1998/01/26 Raw View
hlh_noSPAM@mailexcite.com (Howard Lee Harkness) writes:
> I've run across several instances in Borland's new BCB VCL where base class
> protected members are re-declared public in derived classes. I tried the same
> trick in some of my own code, and it worked. I was under the impression that
> this was non-standard, going by what I understood from Stroupstroup, _The C++
> Programming Language_, 3rd edition, section 15.3.2.2:
>
> "A using-declaration cannot be used to gain access to additional
> information. It is simply a mechanism for making accessible information more
> convenient to use."
>
> However, from Section 11.3 of the CD2:
>
> 1) The access of a member of a base class can be changed in the derived
> class by mentioning its qualified-id in the derived class declaration.
> Such mention is called an access declaration. The effect of an access
> declaration qualified-id ; is defined to be equivalent to the declara-
> tion using qualified-id ;.2)
>
> 2) Access declarations are deprecated; member using-declarations
> (_namespace.udecl_) provide a better means of doing the same things.
> [ ... ]
>
> Does this mean that I merely have to know the name of a protected or private
> base class member in order to break the encapsulation? I hope not -- I hope
> that I am reading something out of context. Explanations would be most
> appreciated.
You can grant access to what you can access yourself to others.
You cannot grant any access you don't have yourself to others.
I suspect that granting public access to a protected member is usually
bad design and/or a result of ignorance. Presumably, the designer of the
base considered the member unsuitable for general use. By granting public
access, the designer of the derived class says that those considerations
doesn't apply to users of the derived class.
Note that a derived class cannot grant access to a private member of its base
and cannot grant access to a protected member of a object that is not of the
derived class.
Also, granting public access to a protected facility was always possible by
using a function:
For example:
class Base {
private:
int priv;
protected:
int prot;
// ...
};
class Derived : public Base {
public:
using Base::priv; // error: Derived cannot access priv
using Base::prot; // ok: Derived can grant access
// to what it can access
int& prot2() { return Base::prot; } // what could have been
// done without 'using'
};
Note that I consider most uses of protected data poor design (see "The C++
Programming Language (3rd edition) sec15.3.1.1 or "The Design and Evolution
of C++" sec13.9).
- Bjarne
Bjarne Stroustrup, AT&T Labs, http://www.research.att.com/~bs
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Michael S. Tsirkin" <mtsirkin@iil.intel.com>
Date: 1998/01/27 Raw View
Howard Lee Harkness wrote:
>
> I've run across several instances in Borland's new BCB VCL where base class
> protected members are re-declared public in derived classes.
[...]
> Stroupstroup, _The C++
> Programming Language_, 3rd edition, section 15.3.2.2:
>
> "A using-declaration cannot be used to gain access to additional
> information. It is simply a mechanism for making accessible information more
> convenient to use."
>
> However, from Section 11.3 of the CD2:
>
> 1) The access of a member of a base class can be changed in the derived
> class by mentioning its qualified-id in the derived class declaration.
> Such mention is called an access declaration. The effect of an access
> declaration qualified-id ; is defined to be equivalent to the declara-
> tion using qualified-id ;.2)
>
> 2) Access declarations are deprecated; member using-declarations
> (_namespace.udecl_) provide a better means of doing the same things.
> [ ... ]
>
> Does this mean that I merely have to know the name of a protected or private
> base class member in order to break the encapsulation? I hope not -- I hope
> that I am reading something out of context. Explanations would be most
> appreciated.
>
> Howard Lee Harkness
Actually you are not breaking encapsulation by this: the protected
members are accessible from the inheriting class anyway.
//Suppose a class with a protected member:
class X {
protected:
int f();
};
//So now instead of:
class Y: public X {
public:
int f_() { return f(); }
};
//you can write:
class Y: public X {
public:
using X::f();
};
which is much nicer IMO.
Regards,
MST
--
Michael S. Tsirkin Intel, Haifa, Israel; Mail stop: IDC-4C
Work telephone in Israel 435-5658 ( Local in Israel 04-8655658 )
mailto:mtsirkin@iil.intel.com ; http://www.iil.intel.com/~mtsirkin/
> Four things are to be strengthened : Torah, and good deeds,
> prayer and one's good manners (Berachoth)
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: hlh_noSPAM@mailexcite.com (Howard Lee Harkness)
Date: 1998/01/23 Raw View
I've run across several instances in Borland's new BCB VCL where base class
protected members are re-declared public in derived classes. I tried the same
trick in some of my own code, and it worked. I was under the impression that
this was non-standard, going by what I understood from Stroupstroup, _The C++
Programming Language_, 3rd edition, section 15.3.2.2:
"A using-declaration cannot be used to gain access to additional
information. It is simply a mechanism for making accessible information more
convenient to use."
However, from Section 11.3 of the CD2:
1) The access of a member of a base class can be changed in the derived
class by mentioning its qualified-id in the derived class declaration.
Such mention is called an access declaration. The effect of an access
declaration qualified-id ; is defined to be equivalent to the declara-
tion using qualified-id ;.2)
2) Access declarations are deprecated; member using-declarations
(_namespace.udecl_) provide a better means of doing the same things.
[ ... ]
Does this mean that I merely have to know the name of a protected or private
base class member in order to break the encapsulation? I hope not -- I hope
that I am reading something out of context. Explanations would be most
appreciated.
Howard Lee Harkness
hlh_NOSPAM@mailexcite.com <--This is NOT MUNGED! It is a valid address!
However, due to excessive spam (spammers don't take hints), I no longer
read mail sent to this address.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Author: hlh_noSPAM@mailexcite.com (Howard Lee Harkness)
Date: 1998/01/24 Raw View
I've run across several instances in Borland's new BCB VCL where base class
protected members are re-declared public in derived classes. I tried the same
trick in some of my own code, and it worked. I was under the impression that
this was non-standard, going by what I understood from Stroupstroup, _The C++
Programming Language_, 3rd edition, section 15.3.2.2:
"A using-declaration cannot be used to gain access to additional
information. It is simply a mechanism for making accessible information more
convenient to use."
However, from Section 11.3 of the CD2:
1) The access of a member of a base class can be changed in the derived
class by mentioning its qualified-id in the derived class declaration.
Such mention is called an access declaration. The effect of an access
declaration qualified-id ; is defined to be equivalent to the declara-
tion using qualified-id ;.2)
2) Access declarations are deprecated; member using-declarations
(_namespace.udecl_) provide a better means of doing the same things.
[ ... ]
Does this mean that I merely have to know the name of a protected or private
base class member in order to break the encapsulation? I hope not -- I hope
that I am reading something out of context. Explanations would be most
appreciated.
Howard Lee Harkness
hlh_NOSPAM@mailexcite.com <--This is NOT MUNGED! It is a valid address!
However, due to excessive spam (spammers don't take hints), I no longer
read mail sent to this address.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/01/25 Raw View
Howard Lee Harkness writes:
> 1) The access of a member of a base class can be changed in the
derived
> class by mentioning its qualified-id in the derived class
declaration.
> Does this mean that I merely have to know the name of a protected or
private
> base class member in order to break the encapsulation?
I don't think so. In my understanding, access refers to what can be
access from outside the class. That is, a class can change the
`export' access of a member. However, if a derived class D cannot
even access the base member B::m, then it just can't re-export it.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]