Default inheritance access specifier
If I have for example two classes A
and B
, such that class B
inherits A
as follows:
class B: public A
In this case, I'm doing public
inheritance.
If I write the previous code as follows:
class B: A
What type of inheritance will I be doing here (i.e; public)? In othe开发者_开发百科r words, what is the default access specifier?
Just a side question here. Do I call the previous line of codes statements
? Especially that I remember I read in the C++ Without Fear: A Beginner's Guide That Makes You Feel Smart book that statements
are that that end with ;
. What do you think about that?
Thanks.
Just a small addition to all the existing answers: the default type of the inheritance depends on the inheriting (derived) type (B
in the example), not on the one that is being inherited (base) (A
in the example).
For example:
class A {};
struct B: /* public */ A {};
struct A {};
class B: /* private */ A {};
It's private for class and public for struct.
Side answer: No, these are definitions of the class according to the standard. Class definition end with a semicolon. On the other hand not all statements end with a semicolon (e.g. an if
statement does not).
When you inherit a class from another class (inherit class Base
from class Derived
in this case), then the default access specifier is private
.
#include <stdio.h>
class Base {
public:
int x;
};
class Derived : Base { }; // is equilalent to class Derived : private Base {}
int main()
{
Derived d;
d.x = 20; // compiler error becuase inheritance is private
getchar();
return 0;
}
When you inherit a class from a structure (inherit class Base
from struct Derived
in this case), then the default access specifier is public
.
#include < stdio.h >
class Base {
public:
int x;
};
struct Derived: Base {}; // is equilalent to struct Derived : public Base {}
int main() {
Derived d;
d.x = 20; // works fine becuase inheritance is public
getchar();
return 0;
}
If you use class
to define your class, the default access specifier will be private
. (I think it's wrong, too.) If you use struct
, however, it will be public
.
And class definitions are declarations, I think. A statement is what translates into actual code (unless optimized away, anyway).
However, a mildly exotic feature of C and C++ is that expressions are statements. That's why 3+4;
is a syntactically legal statement in C++ (although many compilers will warn about it having no effect). While it is obviously nonsense in this case, in general expressions are evaluated for their side effects. (An obvious example is discarding a function's return value. You call the function not to obtain a result, but for its side effects.)
The "type" of inheritance depends on how the class is defined. There are default access specifiers applied to inheritance. From the C++ standard:
[class.access.base]/2
In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key
struct
and private is assumed when the class is defined with the class-keyclass
. [ Example:class B { /* ... */ }; class D1 : private B { /* ... */ }; class D2 : public B { /* ... */ }; class D3 : B { /* ... */ }; // B private by default struct D4 : public B { /* ... */ }; struct D5 : private B { /* ... */ }; struct D6 : B { /* ... */ }; // B public by default class D7 : protected B { /* ... */ }; struct D8 : protected B { /* ... */ };
Here B is a public base of D2, D4, and D6, a private base of D1, D3, and D5, and a protected base of D7 and D8. — end example ]
If you do not choose an inheritance, C++ defaults to private
inheritance in the same way class members default to private
access for classes.
The default type of the inheritance is private in C++.
class B:A
{};
is equivalent to
class B: private A
{};
the default access specifier is an important differentiator between classes and structs. It is public by default for structs and private by default for classes.
AS other casting problem you have
class A { virtual void test() = 0; };
class B : virtual public A { virtual void testb() {} };
class C : virtual public A { virtual void testc() {} };
class D : public B, public C {
virtual void test() override {}
}
void main() {
D d;
void* v = &d;
A* a = &d;
((D*)A)->test(); //OK
((D*)v)->test(); //undefined behavior (that call testb() in vtable logic at 1st inheritance position)
dynamic_cast<D*>(v)->test(); //compile error cast from void* not permitted
//resolution
void* x = a;
((D*)x)->test(); //OK but as you can see, you must to store a* in x*
}
精彩评论