Home > Article > Backend Development > Why Are Public Class Members Invisible When Inheriting Templated C Classes?
Inheriting Protected and Public Class Member Visibility in Templated C Classes
The issue of public member visibility in inheritance becomes apparent when a class template inherits members from another class template, as exemplified in the provided code. Without explicitly redeclaring or redefining any public members, CDerived inherits all public members of CBase.
Problem and Solutions
Templating this code, however, reveals that public members of CBase become invisible to CDerived on compilers adhering to the latest C standard. This visibility issue can be addressed using several solutions:
Drawbacks of Existing Solutions
Although effective, these solutions have their drawbacks:
A Less Verbose Solution
Introducing macros simplifies Solution #3, reducing repetitive typing:
<code class="cpp">#include <boost/preprocessor.hpp> #define USING_ONE(r, base, member) using base::member; #define USING_ALL(base, ...) BOOST_PP_SEQ_FOR_EACH(USING_ONE, base, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) // Near CBase<BYTES> #define USING_CBASE(param) USING_ALL(CBase<param>, Arr, Fn1, Fn2, Fn3, Fn4, Fn5) // In CDerived<BYTES> USING_CBASE(BYTES);</code>
This code greatly simplifies the using statements, improving code readability and reducing repetitive edits.
The above is the detailed content of Why Are Public Class Members Invisible When Inheriting Templated C Classes?. For more information, please follow other related articles on the PHP Chinese website!