Re: What exactly is considered inherited from a base class?
On 2011-05-13 01:53:29 -0400, Kris Prad said:
In case a derived class is a ???friend???, then everything in the base
class is inherited. Otherwise, private members are not inherited.
No. Everything (almost; see below) is inherited. Some inherited names
might not be accessible, but that's a separate question. As I said,
*explore* it.
However, consider the following example where even the public members
cannot be said to be inherited:
struct X {};
struct Base
{
void f(const X&) { }
};
struct Derived : public Base
{
// using Base::f; // required if we need to call Base::f(..)
void f(int) { } // hides Base::f, unless using brings it to scope
};
int main(int argc, char** argv)
{
Derived d;
d.f(X()); // fails to compile without ???using??? in Derived
}
While ???friend??? makes private members accessible, ???using??? makes public
members accessible, by bringing the hidden methods into scope.
No, using doesn't affect inheritance. Inheritance is about whether
names from the base class are *visible* in the derived class. Access to
those names is separate from inheritance, and is controlled by access
declarations: public, protected, private. And using directives and
declarations are about overload resolution, not inheritance.
The
former is specified in the base class, and the latter in the derived.
See another blanket definition here:
http://www.cplusplus.com/doc/tutorial/inheritance/
???What is inherited from the base class?
In principle, a derived class inherits every member of a base class
except: Its constructor and its destructor, its operator=() members,
its friends???
Constructors, destructors, and assignment operators are not inherited.
I have seen students getting confused when they know a derived object
physically contains all the members of the base class, but are told
either 1) the derived inherits only part of them or 2) inherits all
but access only a part. Which is right?
So, my understanding of what is inherited:
Whatever the derived class can access. This depends on access
specifiers, ???friend??? and ???using??? declarations.
That's a very confusing definition of inheritance, since it mixes
together inheritance, access, and overloading.
This works for me, but
not sure this is the correct way to teach some one.:-)
class Base {
int i; // private
};
class Derived : public Base {
};
Derived d;
d.i = 3; // Error: i is private
Try it.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]