Why does C++ forbid private inheritance of a final class?
C++11 introduced the final
keyword to C++.
It can be used on a virtual method or on a class.
Declaring a class final forbids any kind of inheritance: public, protected and private.
struct A final
;
class B: private A
;
error: base 'A' ^ is marked 'final'
While it can be reasonable to forbid public inheritance (e.g. if my class doesn't have a virtual destructor, or for other reasons), why should I forbid private inheritance?
Might it be that if final
forbade only public inheritance, that std::string
and its other friends in std would have been final
-- as they should -- for not having a virtual destructor?
EDIT:
Howard Hinnant already answered Why the standard containers are not final but still, there is a reason for declaring a class final but allowing private inheritance.
c++ final
add a comment |Â
C++11 introduced the final
keyword to C++.
It can be used on a virtual method or on a class.
Declaring a class final forbids any kind of inheritance: public, protected and private.
struct A final
;
class B: private A
;
error: base 'A' ^ is marked 'final'
While it can be reasonable to forbid public inheritance (e.g. if my class doesn't have a virtual destructor, or for other reasons), why should I forbid private inheritance?
Might it be that if final
forbade only public inheritance, that std::string
and its other friends in std would have been final
-- as they should -- for not having a virtual destructor?
EDIT:
Howard Hinnant already answered Why the standard containers are not final but still, there is a reason for declaring a class final but allowing private inheritance.
c++ final
8
Not having a virtual destructor does not prohibit or limit inheritance anyhow. It only makes ownership over object through a pointer to base class faulty. So that is not a reason for makingstd::string
and most of other library classesfinal
.
â VTT
22 hours ago
How would that hypothetical feature work? Assume you can derive (if only privately) from a final class. Would virtual functions be treated as final?
â curiousguy
16 hours ago
add a comment |Â
C++11 introduced the final
keyword to C++.
It can be used on a virtual method or on a class.
Declaring a class final forbids any kind of inheritance: public, protected and private.
struct A final
;
class B: private A
;
error: base 'A' ^ is marked 'final'
While it can be reasonable to forbid public inheritance (e.g. if my class doesn't have a virtual destructor, or for other reasons), why should I forbid private inheritance?
Might it be that if final
forbade only public inheritance, that std::string
and its other friends in std would have been final
-- as they should -- for not having a virtual destructor?
EDIT:
Howard Hinnant already answered Why the standard containers are not final but still, there is a reason for declaring a class final but allowing private inheritance.
c++ final
C++11 introduced the final
keyword to C++.
It can be used on a virtual method or on a class.
Declaring a class final forbids any kind of inheritance: public, protected and private.
struct A final
;
class B: private A
;
error: base 'A' ^ is marked 'final'
While it can be reasonable to forbid public inheritance (e.g. if my class doesn't have a virtual destructor, or for other reasons), why should I forbid private inheritance?
Might it be that if final
forbade only public inheritance, that std::string
and its other friends in std would have been final
-- as they should -- for not having a virtual destructor?
EDIT:
Howard Hinnant already answered Why the standard containers are not final but still, there is a reason for declaring a class final but allowing private inheritance.
c++ final
c++ final
edited 18 hours ago
Boann
36.7k1287121
36.7k1287121
asked 22 hours ago
Amir Kirsh
1,338819
1,338819
8
Not having a virtual destructor does not prohibit or limit inheritance anyhow. It only makes ownership over object through a pointer to base class faulty. So that is not a reason for makingstd::string
and most of other library classesfinal
.
â VTT
22 hours ago
How would that hypothetical feature work? Assume you can derive (if only privately) from a final class. Would virtual functions be treated as final?
â curiousguy
16 hours ago
add a comment |Â
8
Not having a virtual destructor does not prohibit or limit inheritance anyhow. It only makes ownership over object through a pointer to base class faulty. So that is not a reason for makingstd::string
and most of other library classesfinal
.
â VTT
22 hours ago
How would that hypothetical feature work? Assume you can derive (if only privately) from a final class. Would virtual functions be treated as final?
â curiousguy
16 hours ago
8
8
Not having a virtual destructor does not prohibit or limit inheritance anyhow. It only makes ownership over object through a pointer to base class faulty. So that is not a reason for making
std::string
and most of other library classes final
.â VTT
22 hours ago
Not having a virtual destructor does not prohibit or limit inheritance anyhow. It only makes ownership over object through a pointer to base class faulty. So that is not a reason for making
std::string
and most of other library classes final
.â VTT
22 hours ago
How would that hypothetical feature work? Assume you can derive (if only privately) from a final class. Would virtual functions be treated as final?
â curiousguy
16 hours ago
How would that hypothetical feature work? Assume you can derive (if only privately) from a final class. Would virtual functions be treated as final?
â curiousguy
16 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
Inheritance is inheritance. Accessibility is orthogonal to it. It only protects from statically treating the derived class as the base, outside the scope of the derived class. It makes no difference at runtime, and if private inheritance was allowed, you could write this:
struct C
virtual void foo()
;
struct A final : C
virtual void foo()
;
void baz(A& ref) ref.foo();
class B: private A
virtual void foo()
void bar()
baz(*this);
;
Private inheritance doesn't stop you from using run-time polymorphism. If final
is meant to fully prevent further overriding, then private inheritance must be included in the prohibition.
Beautiful example, which reminds me the peculiar rule: if B is privately inherited from A, object of type B is still considered "an A" inside B itself! IMHO it would be better if the spec would not allow B in this case (of private inheritance) to override private virtuals of A, even without final! But this would be an extra rule for a very rare case I guess... and maybe not a good one at all. A better rule might be: be careful with private inheritance.
â Amir Kirsh
22 hours ago
1
@AmirKirsh - A subsection of the master rule. Be careful with C++ in general :)
â StoryTeller
22 hours ago
2
privately inherited from the master rule? :-)
â Amir Kirsh
22 hours ago
It could have been possible to havefinal
only cap the polymorphic hierarchy. In that case, accessibility wouldn't have come into it either though.
â Deduplicator
18 hours ago
@Deduplicator - Could have. But the paper proposing it wanted to give class authors the tool to say "you can't inherit from this, period". That's not without merit either, and that's the direction the committee went with.
â StoryTeller
18 hours ago
add a comment |Â
In addition to what Story Teller said, consider the reason for introducing final
: It's supposed to help optimizations.
When a class is final
, and you have a pointer to it, the compiler can prove which member function you are calling, even if it's virtual
. If the class is not final
, the pointer could actually be a pointer to some derived class, which could conceivably override the virtual
method, forcing a full dynamic vtable lookup.
Whether the inheritance is private
or not, it is always possible to create a base-class pointer. In the case of private
inheritance, the creation of this base-class pointer would be restricted to the deriving class, the derived class, and any base of the derived class, which is still more code than the optimizer has available to make its decisions. As such, only forbidding all inheritance allows the virtual call optimizations to be made.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Inheritance is inheritance. Accessibility is orthogonal to it. It only protects from statically treating the derived class as the base, outside the scope of the derived class. It makes no difference at runtime, and if private inheritance was allowed, you could write this:
struct C
virtual void foo()
;
struct A final : C
virtual void foo()
;
void baz(A& ref) ref.foo();
class B: private A
virtual void foo()
void bar()
baz(*this);
;
Private inheritance doesn't stop you from using run-time polymorphism. If final
is meant to fully prevent further overriding, then private inheritance must be included in the prohibition.
Beautiful example, which reminds me the peculiar rule: if B is privately inherited from A, object of type B is still considered "an A" inside B itself! IMHO it would be better if the spec would not allow B in this case (of private inheritance) to override private virtuals of A, even without final! But this would be an extra rule for a very rare case I guess... and maybe not a good one at all. A better rule might be: be careful with private inheritance.
â Amir Kirsh
22 hours ago
1
@AmirKirsh - A subsection of the master rule. Be careful with C++ in general :)
â StoryTeller
22 hours ago
2
privately inherited from the master rule? :-)
â Amir Kirsh
22 hours ago
It could have been possible to havefinal
only cap the polymorphic hierarchy. In that case, accessibility wouldn't have come into it either though.
â Deduplicator
18 hours ago
@Deduplicator - Could have. But the paper proposing it wanted to give class authors the tool to say "you can't inherit from this, period". That's not without merit either, and that's the direction the committee went with.
â StoryTeller
18 hours ago
add a comment |Â
Inheritance is inheritance. Accessibility is orthogonal to it. It only protects from statically treating the derived class as the base, outside the scope of the derived class. It makes no difference at runtime, and if private inheritance was allowed, you could write this:
struct C
virtual void foo()
;
struct A final : C
virtual void foo()
;
void baz(A& ref) ref.foo();
class B: private A
virtual void foo()
void bar()
baz(*this);
;
Private inheritance doesn't stop you from using run-time polymorphism. If final
is meant to fully prevent further overriding, then private inheritance must be included in the prohibition.
Beautiful example, which reminds me the peculiar rule: if B is privately inherited from A, object of type B is still considered "an A" inside B itself! IMHO it would be better if the spec would not allow B in this case (of private inheritance) to override private virtuals of A, even without final! But this would be an extra rule for a very rare case I guess... and maybe not a good one at all. A better rule might be: be careful with private inheritance.
â Amir Kirsh
22 hours ago
1
@AmirKirsh - A subsection of the master rule. Be careful with C++ in general :)
â StoryTeller
22 hours ago
2
privately inherited from the master rule? :-)
â Amir Kirsh
22 hours ago
It could have been possible to havefinal
only cap the polymorphic hierarchy. In that case, accessibility wouldn't have come into it either though.
â Deduplicator
18 hours ago
@Deduplicator - Could have. But the paper proposing it wanted to give class authors the tool to say "you can't inherit from this, period". That's not without merit either, and that's the direction the committee went with.
â StoryTeller
18 hours ago
add a comment |Â
Inheritance is inheritance. Accessibility is orthogonal to it. It only protects from statically treating the derived class as the base, outside the scope of the derived class. It makes no difference at runtime, and if private inheritance was allowed, you could write this:
struct C
virtual void foo()
;
struct A final : C
virtual void foo()
;
void baz(A& ref) ref.foo();
class B: private A
virtual void foo()
void bar()
baz(*this);
;
Private inheritance doesn't stop you from using run-time polymorphism. If final
is meant to fully prevent further overriding, then private inheritance must be included in the prohibition.
Inheritance is inheritance. Accessibility is orthogonal to it. It only protects from statically treating the derived class as the base, outside the scope of the derived class. It makes no difference at runtime, and if private inheritance was allowed, you could write this:
struct C
virtual void foo()
;
struct A final : C
virtual void foo()
;
void baz(A& ref) ref.foo();
class B: private A
virtual void foo()
void bar()
baz(*this);
;
Private inheritance doesn't stop you from using run-time polymorphism. If final
is meant to fully prevent further overriding, then private inheritance must be included in the prohibition.
edited 18 hours ago
Boann
36.7k1287121
36.7k1287121
answered 22 hours ago
StoryTeller
93.4k12187253
93.4k12187253
Beautiful example, which reminds me the peculiar rule: if B is privately inherited from A, object of type B is still considered "an A" inside B itself! IMHO it would be better if the spec would not allow B in this case (of private inheritance) to override private virtuals of A, even without final! But this would be an extra rule for a very rare case I guess... and maybe not a good one at all. A better rule might be: be careful with private inheritance.
â Amir Kirsh
22 hours ago
1
@AmirKirsh - A subsection of the master rule. Be careful with C++ in general :)
â StoryTeller
22 hours ago
2
privately inherited from the master rule? :-)
â Amir Kirsh
22 hours ago
It could have been possible to havefinal
only cap the polymorphic hierarchy. In that case, accessibility wouldn't have come into it either though.
â Deduplicator
18 hours ago
@Deduplicator - Could have. But the paper proposing it wanted to give class authors the tool to say "you can't inherit from this, period". That's not without merit either, and that's the direction the committee went with.
â StoryTeller
18 hours ago
add a comment |Â
Beautiful example, which reminds me the peculiar rule: if B is privately inherited from A, object of type B is still considered "an A" inside B itself! IMHO it would be better if the spec would not allow B in this case (of private inheritance) to override private virtuals of A, even without final! But this would be an extra rule for a very rare case I guess... and maybe not a good one at all. A better rule might be: be careful with private inheritance.
â Amir Kirsh
22 hours ago
1
@AmirKirsh - A subsection of the master rule. Be careful with C++ in general :)
â StoryTeller
22 hours ago
2
privately inherited from the master rule? :-)
â Amir Kirsh
22 hours ago
It could have been possible to havefinal
only cap the polymorphic hierarchy. In that case, accessibility wouldn't have come into it either though.
â Deduplicator
18 hours ago
@Deduplicator - Could have. But the paper proposing it wanted to give class authors the tool to say "you can't inherit from this, period". That's not without merit either, and that's the direction the committee went with.
â StoryTeller
18 hours ago
Beautiful example, which reminds me the peculiar rule: if B is privately inherited from A, object of type B is still considered "an A" inside B itself! IMHO it would be better if the spec would not allow B in this case (of private inheritance) to override private virtuals of A, even without final! But this would be an extra rule for a very rare case I guess... and maybe not a good one at all. A better rule might be: be careful with private inheritance.
â Amir Kirsh
22 hours ago
Beautiful example, which reminds me the peculiar rule: if B is privately inherited from A, object of type B is still considered "an A" inside B itself! IMHO it would be better if the spec would not allow B in this case (of private inheritance) to override private virtuals of A, even without final! But this would be an extra rule for a very rare case I guess... and maybe not a good one at all. A better rule might be: be careful with private inheritance.
â Amir Kirsh
22 hours ago
1
1
@AmirKirsh - A subsection of the master rule. Be careful with C++ in general :)
â StoryTeller
22 hours ago
@AmirKirsh - A subsection of the master rule. Be careful with C++ in general :)
â StoryTeller
22 hours ago
2
2
privately inherited from the master rule? :-)
â Amir Kirsh
22 hours ago
privately inherited from the master rule? :-)
â Amir Kirsh
22 hours ago
It could have been possible to have
final
only cap the polymorphic hierarchy. In that case, accessibility wouldn't have come into it either though.â Deduplicator
18 hours ago
It could have been possible to have
final
only cap the polymorphic hierarchy. In that case, accessibility wouldn't have come into it either though.â Deduplicator
18 hours ago
@Deduplicator - Could have. But the paper proposing it wanted to give class authors the tool to say "you can't inherit from this, period". That's not without merit either, and that's the direction the committee went with.
â StoryTeller
18 hours ago
@Deduplicator - Could have. But the paper proposing it wanted to give class authors the tool to say "you can't inherit from this, period". That's not without merit either, and that's the direction the committee went with.
â StoryTeller
18 hours ago
add a comment |Â
In addition to what Story Teller said, consider the reason for introducing final
: It's supposed to help optimizations.
When a class is final
, and you have a pointer to it, the compiler can prove which member function you are calling, even if it's virtual
. If the class is not final
, the pointer could actually be a pointer to some derived class, which could conceivably override the virtual
method, forcing a full dynamic vtable lookup.
Whether the inheritance is private
or not, it is always possible to create a base-class pointer. In the case of private
inheritance, the creation of this base-class pointer would be restricted to the deriving class, the derived class, and any base of the derived class, which is still more code than the optimizer has available to make its decisions. As such, only forbidding all inheritance allows the virtual call optimizations to be made.
add a comment |Â
In addition to what Story Teller said, consider the reason for introducing final
: It's supposed to help optimizations.
When a class is final
, and you have a pointer to it, the compiler can prove which member function you are calling, even if it's virtual
. If the class is not final
, the pointer could actually be a pointer to some derived class, which could conceivably override the virtual
method, forcing a full dynamic vtable lookup.
Whether the inheritance is private
or not, it is always possible to create a base-class pointer. In the case of private
inheritance, the creation of this base-class pointer would be restricted to the deriving class, the derived class, and any base of the derived class, which is still more code than the optimizer has available to make its decisions. As such, only forbidding all inheritance allows the virtual call optimizations to be made.
add a comment |Â
In addition to what Story Teller said, consider the reason for introducing final
: It's supposed to help optimizations.
When a class is final
, and you have a pointer to it, the compiler can prove which member function you are calling, even if it's virtual
. If the class is not final
, the pointer could actually be a pointer to some derived class, which could conceivably override the virtual
method, forcing a full dynamic vtable lookup.
Whether the inheritance is private
or not, it is always possible to create a base-class pointer. In the case of private
inheritance, the creation of this base-class pointer would be restricted to the deriving class, the derived class, and any base of the derived class, which is still more code than the optimizer has available to make its decisions. As such, only forbidding all inheritance allows the virtual call optimizations to be made.
In addition to what Story Teller said, consider the reason for introducing final
: It's supposed to help optimizations.
When a class is final
, and you have a pointer to it, the compiler can prove which member function you are calling, even if it's virtual
. If the class is not final
, the pointer could actually be a pointer to some derived class, which could conceivably override the virtual
method, forcing a full dynamic vtable lookup.
Whether the inheritance is private
or not, it is always possible to create a base-class pointer. In the case of private
inheritance, the creation of this base-class pointer would be restricted to the deriving class, the derived class, and any base of the derived class, which is still more code than the optimizer has available to make its decisions. As such, only forbidding all inheritance allows the virtual call optimizations to be made.
answered 18 hours ago
cmaster
25.7k63778
25.7k63778
add a comment |Â
add a comment |Â
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53920492%2fwhy-does-c-forbid-private-inheritance-of-a-final-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
8
Not having a virtual destructor does not prohibit or limit inheritance anyhow. It only makes ownership over object through a pointer to base class faulty. So that is not a reason for making
std::string
and most of other library classesfinal
.â VTT
22 hours ago
How would that hypothetical feature work? Assume you can derive (if only privately) from a final class. Would virtual functions be treated as final?
â curiousguy
16 hours ago