Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unshadow global identifier in a class

I have the following C++ code:

class ident {};

class Base {
public:
  int ident;
};

class Derived : public Base {
  ::ident var; // refers to the global identifier
  ident var; // syntax error, refers to the base member
};

Is it possible to make it so ident refers to the global variable in Derived::method()? Eventually completely hiding Base::ident? I cannot change Base, but I can make an intermediate class. I can add lines everywhere but the generation of the member variables will always refer to ident as ident.

It is a very unusual request, because it is for a templated code generator (SWIG).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you make the base depend on a template parameter:

template <typename T>
class DerivedLow : public T
{
    void method()
    {
        ident = 0; // Refers to the global entity.
    }
};

using Derived = DerivedLow<Base>;

Then nothing from the base class will be accessible in the derived class by default. You can still access base members with this-> or Base::, or by importing them with using.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading