Topic: Making private members protected in derived class


Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/09/12
Raw View
In article <rtj0leav1iv71@corp.supernews.com>, Maxim Veselyy
<MVeselyy@software-automation.com> writes
>
>I remember seeing it somewhere that when deriving the class with private
>members you can change Access Privileges to a member by declaring in public
>or protected section of derived class .
>
>class Base {
>  int a;
>};
>class Derived : public Base {
> public :
>  int Base::a;
>
>};
>
>for some reason it does not work with VC++ 6.0.

Nor should it. It makes a world of difference whether you are dealing
with private or protected access.  Derived has no access privileges to
private items in Base.

Change Base to
class Base {
protected:
        int a;
};

and update Derived to the modern idiom:


class Derived: public Base {
public:
        using Base::a;
};

and you should not get a complaint from the compiler.  However you will
get strong complaints from competent C++ programmers that you need quite
exceptional reasons to do such a thing.



>

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Maxim Veselyy" <MVeselyy@software-automation.com>
Date: 1999/09/10
Raw View
I remember seeing it somewhere that when deriving the class with private
members you can change Access Privileges to a member by declaring in public
or protected section of derived class .

class Base {
  int a;
};
class Derived : public Base {
 public :
  int Base::a;

};

for some reason it does not work with VC++ 6.0.





[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: brahms@mindspring.com (Stan Brown)
Date: 1999/09/11
Raw View
MVeselyy@software-automation.com (Maxim Veselyy) wrote in comp.std.c++:
>I remember seeing it somewhere that when deriving the class with private
>members you can change Access Privileges to a member by declaring in public
>or protected section of derived class .

I don't think so. According to Stroustrup's D&E, one of the basic
principles of C++ is that access can only be granted, never unilaterally
taken.

Therefore, if I understand correctly, no private members of a base class
are accessible to a derived class unless the base class makes the derived
class a friend.

--
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
                                    http://www.mindspring.com/~brahms/
I don't need e-mail copies of posted follow-ups, but if you send
them PLEASE identify them as such.
My reply address is correct as is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]