C ++ replaces access for member function with overload

My question is related to this: How to publicly inherit from a base class, but make some public methods from the base class private in the derived class? but my case is a little more complicated as I want to change access for overloaded / template functions:

template<typename T> class Base { public: void fun(int); //1 void fun(float); //2 void fun(T); //3 template<typename U> //4 void fun(U); }; template<typename T1, typename T2> class Derived : private Base<T1> { public: //change access specifier here for: //1 - make fun(int) available //2 - fun(float) is really evil and it should not be accessible! //3 - make fun(T1) == Base::fun(T) available //4 - make template<typename U> void Base::fun(U) available }; 

I tried the method from the previous answer to make some function public, but I get this error:

 ISO C++11 does not allow access declarations; use using declarations instead 

How can I use using to make available only to selected functions (1, 3 and 4) for Derived users?

+5
source share
3 answers

As Armen and Jarod noted, using will bring all the fun to the derived class. Jarod had a great idea: delete an evil function! By combining their answers, I got the following:

 template<typename T1, typename T2> class Derived : private Base<T1> { public: using Base<T1>::fun; void fun(float) = delete; }; 

which does exactly what I originally wanted!

+6
source

You can either make all objects named fun available, or none of them. To do this, write:

 public: using Base::fun; 
+3
source

In your case, you need to direct manually, since using works in general:

 template<typename T1, typename T2> class Derived : private Base<T1> { public: void fun(int i) { Base<T1>::fun(i); } // Don't show void fun(float); so fun(float) will call the template method // or forbid fun(float) by deleting the function // void fun(float) = delete; void fun(T1 t) { Base<T1>::fun(t); } template<typename U> void fun(U u) { Base<T1>::fun(u); } }; 
+2
source

Source: https://habr.com/ru/post/1204560/


All Articles