Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ change access for member function with overloading

Tags:

c++

c++11

c++14

My question is related to this one: How to publicly inherit from a base class but make some of public methods from the base class private in the derived class?, but my case is a little more complex as I want to change the 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've tried the method from 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 only the selected functions (1, 3 and 4) available for users of Derived?

like image 209
Mircea Ispas Avatar asked Oct 13 '14 13:10

Mircea Ispas


People also ask

Can member functions be overloaded?

Function declarations that differ only by its return type cannot be overloaded with function overloading process. Member function declarations with the same parameters or the same name types cannot be overloaded if any one of them is declared as a static member function.

Can you overload IO operator using member function?

You can't make the overloaded operator polymorphic itself, but you make the member function it calls virtual , so it acts polymorphic anyway.

Which operator is required to be overloaded as a member function only?

Explaination. Overloaded assignment operator does the job similar to copy constructor and is required to be overloaded as member function of the class.

How do you overload operators using member function and using friend function explain the rules?

In the case of a friend function, the binary operator should have only two argument and unary should have only one argument. All the class member object should be public if operator overloading is implemented. Operators that cannot be overloaded are . . * :: ?:


1 Answers

As Armen and Jarod pointed out, using will bring all fun's in the derived class. Jarod though had a great idea: delete the evil function! Combining their answers I got this:

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

which does exactly what I wanted originally!

like image 103
Mircea Ispas Avatar answered Oct 02 '22 03:10

Mircea Ispas