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

Is there a way to use the "using" keyword to refer to a const function only from the base class?

If the base class has both const and non-const version of a function, can I refer to only one or the other in the derived class by the using keyword?

struct Base
{
protected:
    int x = 1;

    const int&  getX() const     {return x;}
    int&        getX()           {return x;}
};

struct Derived : public Base
{
public:
    using Base::getX; // Can we refer to the const or the non-const only?
};

If no, whats the simplest way to make const getX() public in Derived without repeating the function body?

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 :

using always brings the entire overload set for the given name with it. You cannot invoke using for a particular function, only for its name, which includes all uses of that name.

You will have to write a derived-class version of the function, with your preferred signature, that calls the base class. The simplest way would be with decltype(auto) getX() const { return Base::getX(); }.

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