Home  >  Article  >  Backend Development  >  How Can I Avoid Member Invisibility and Source Code Bloat in Inherited Class Templates?

How Can I Avoid Member Invisibility and Source Code Bloat in Inherited Class Templates?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-01 05:38:27121browse

How Can I Avoid Member Invisibility and Source Code Bloat in Inherited Class Templates?

Avoiding Member Invisibility and Source Code Bloat with Inherited Class Templates

The problem encountered occurs when a class template, CDerived, inherits from a base class template, CBase, and all public members of CBase become invisible to CDerived when the code is compiled with standard C conformance enabled.

Solutions to the Problem:

Solution #1: Explicit Qualification

Prefix references to CBase members in CDerived with CBase::, e.g., Fn1() becomes CBase::Fn1(). This forces name lookup to occur in the base class scope, making the members visible. However, this solution requires many verbose additions to the code, leading to source code bloat.

Solution #2: this-> Prefix

Prefix references to CBase members with this->, e.g., Fn1() becomes this->Fn1(). This also makes the members visible, but again requires verbose code alterations.

Solution #3: Using Statements

Add using statements inside CDerived to import CBase members, e.g., using CBase::Fn1; using CBase::Fn2;. This requires only one using statement per member used in CDerived, but a blanket statement to import all members does not exist.

Solution #4: Non-Standard Permissive Mode

Disable strict C conformance in the compiler settings to suppress the name lookup issue. However, this is a global solution that breaks away from the standard and is not portable.

A More Concise Solution #5:

To reduce the verbosity of Solution #3, macros can be used to generate the necessary using statements.

Implementation:

Include the Boost preprocessor library and define a macro, USING_ONE, to generate a single using statement for a specific base member.

Define another macro, USING_ALL, to generate a sequence of using statements for all specified base members.

In the base class template, define a macro, USING_CBASE, to generate the using statements for all protected and public members that will be used in derived class templates.

Within the derived class template, use the appropriate USING_CBASE macro to import the necessary members using the macros defined earlier. This process requires minimal searching and typing, reducing source code bloat while still maintaining portability.

The above is the detailed content of How Can I Avoid Member Invisibility and Source Code Bloat in Inherited Class Templates?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn